NSJSONSerialization fails at DBL_MAX

I ran into the problem of JSON deserialization containing a big double. Basically, I can serialize a dictionary like this

NSDictionary *dict = @{@"doubleVal": @DBL_MAX}; 

which serializes without problems

 {"doubleVal":1.797693134862316e+308} 

However, if I then try to deserialize the data, it will explode.

A small unit test that demonstrates the problem:

 - (void)test_600_doubleJSON { NSDictionary *dict = @{@"doubleVal": @DBL_MAX}; NSError *jsonError, *jsonResponseError; NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:&jsonError]; id jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonResponseError]; STAssertNil(jsonError, @"JSON error: %@", jsonError.localizedDescription); STAssertNil(jsonResponseError, @"JSON response error: %@", jsonResponseError.localizedDescription); } 

The last statement fails:

 JSON response error: The operation couldn't be completed. (Cocoa error 3840.) 

If I replace DBL_MAX with a (much) lower value, the test will complete as expected.

+7
source share

All Articles