NSJSONSerialization incorrectly decodes large numbers as double

Originator:jose.ibanez
Number:rdar://18666669 Date Originated:15-Oct-2014
Status:Open Resolved:
Product:iOS Product Version:iOS 7, iOS 8
Classification:Serious Issue Reproducible:Always
 
Summary:
NSJSONSerialization incorrectly decodes numeric values with at least 19 digits as doubles instead of long long.

A JSON string such as:

    {"number":999999999999999999}

will produce an NSNumber with an objCType of `q`, or long long. The value returned from `- (long long)longLongValue` will be exactly 999999999999999999.

However, a JSON string such as:

    {"number":1000000000000000001}

will produce an NSNumber with an objCType of `d`, or double. This can result in a loss of precision when extracting the value using `- (long long)longLongValue` on iOS 7, or when serializing the value based on the objCType.

Steps to Reproduce:
Open the attached project and run the tests.

Expected Results:
Numbers in JSON data less than LONG_LONG_MAX that do not have a decimal point should be decoded into NSNumbers with long long objCType.

Actual Results:
Numbers in JSON data with at least 19 digits result in NSNumber objects with an objCType of double.

Version:
iOS 7, iOS 8

Notes:


Configuration:
iPhone 5s, iPad 3, iPad Air, iPhone 5

Attachments:
https://github.com/jose-ibanez/JSONNumbers

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!