Fixed double number JSON serialization to encode 17 significant digits (this

matches the max number of significant digits in the double-precision floating 
point format). This prevents a loss of precision, in case the number has more 
than 3 digits after the decimal point.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@37556 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Quentin Mathe 2014-01-07 19:20:00 +00:00
parent 1bd5683eea
commit 150289de30
3 changed files with 13 additions and 2 deletions

View file

@ -1,3 +1,13 @@
2014-01-07 Quentin Mathe <quentin.mathe@gmail.com>
* Source/NSJSONSerialization.m (writeObject()): Fixed double number
serialization to encode 17 significant digits (this matches the max number
of significant digits in the double-precision floating point format). This
prevents a loss of precision, in case the number has more than 3 digits
after the decimal point.
* Tests/base/NSJSONSerialization/json.m: Updated to test double number
serialization.
2014-01-07 Richard Frith-Macdonald <rfm@gnu.org> 2014-01-07 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSOperation.m: Make sure current queue is set for threads * Source/NSOperation.m: Make sure current queue is set for threads

View file

@ -901,7 +901,7 @@ writeObject(id obj, NSMutableString *output, NSInteger tabs)
} }
else else
{ {
[output appendFormat: @"%g", [(NSNumber*)obj doubleValue]]; [output appendFormat: @"%.17g", [(NSNumber*)obj doubleValue]];
} }
} }
else if ([obj isKindOfClass: NSNullClass]) else if ([obj isKindOfClass: NSNullClass])

View file

@ -21,6 +21,7 @@ int main(void)
\"emptyString\":\"\",\"emptyArray\":[],\"emptyObject\":{},\ \"emptyString\":\"\",\"emptyArray\":[],\"emptyObject\":{},\
\"IDs\": [116, 943, 234, 38793],\ \"IDs\": [116, 943, 234, 38793],\
\"escapeTest\": \"\\\"\\u0001\"\ \"escapeTest\": \"\\\"\\u0001\"\
\"double\": 123.456789012254,\
}"; }";
NSStringEncoding encs[] = {\ NSStringEncoding encs[] = {\
NSUTF8StringEncoding,\ NSUTF8StringEncoding,\
@ -48,7 +49,7 @@ int main(void)
obj = tmp; obj = tmp;
} }
PASS([obj count] == 6, PASS([obj count] == 7,
"Decoded dictionary had the right number of elements"); "Decoded dictionary had the right number of elements");
PASS([NSJSONSerialization isValidJSONObject: obj], PASS([NSJSONSerialization isValidJSONObject: obj],
"Can serialise deserialised JSON"); "Can serialise deserialised JSON");