Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integral double values are encoded as integers #63

Open
arschles opened this issue Nov 22, 2011 · 3 comments
Open

Integral double values are encoded as integers #63

arschles opened this issue Nov 22, 2011 · 3 comments

Comments

@arschles
Copy link

if I run this code:

static NSString * key = @"doubleKey";
NSNumber * doubleNumber = [NSNumber numberWithDouble:22];
NSMutableDictionary * dict = [NSMutableDictionary dictionary];
[dict setObject:doubleNumber forKey:key];
NSLog("%@", [dict JSONString]);

I get this json:

{"doubleKey":22}

I'm not intimately familiar with the JSON spec, but this encoding was causing me some problems where I expected a double to come back on decode, but an int came back.

I implemented a possible fix locally, but I'd like to hear feedback before I fork and submit a pull request. The fix is to change the format string at https://github.com/johnezang/JSONKit/blob/master/JSONKit.m#L2755, which is currently "%.17g" to "%#.17g" in order to force trailing zeroes for double values that are also whole numbers.

@johnezang
Copy link
Owner

This is a sticky corner case. It is, however, documented behavior in the README.md (queue trumpets blaring). The passage in particular is:

Floating-point numbers are converted to their decimal representation using the printf format conversion specifier %.17g. Theoretically this allows up to a float, or IEEE 754 Single 32-bit floating-point, worth of precision to be represented. This means that for practical purposes, double values are converted to float values with the associated loss of precision. The RFC 4627 standard is silent on how floating-point numbers should be dealt with and the author has found that real world JSON implementations vary wildly in how they handle this issue. Furthermore, the %g format conversion specifier may convert floating-point values that can be exactly represented as an integer to a textual representation that does not include a . or e– essentially silently promoting a floating-point value to an integer value (i.e, 5.0 becomes 5). Because of these and many other issues surrounding the conversion and manipulation of floating-point values, you should not expect or depend on floating point values to maintain their full precision, or when round tripped, to compare equal.

The very short answer: You're pretty much screwed. Fundamentally and throughly.

From the printf documentation, I note the following (wrt/ # modifier):

For g and G conversions, trailing zeros are not removed from the result as they would otherwise be.

Yuck. So basically every single floating point number becomes 17 digits long. The cure is worst than the disease...

I'm sympathetic, but there's no obvious Right Thing(tm) to do, or way to handle it. :(

@ghost ghost assigned johnezang Nov 27, 2011
@arschles
Copy link
Author

Hey John,

I understand and agree with pretty much everything you said, except that the cure is worse than the disease. I think that it's only slightly better than the disease, but my opinion may change :). I'm going to fork and make the change to add the # modifier for now.

@arschles
Copy link
Author

Thanks for your response, by the way

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants