From 150289de30282d6fb4f08031a1cd22a8ba539572 Mon Sep 17 00:00:00 2001 From: Quentin Mathe Date: Tue, 7 Jan 2014 19:20:00 +0000 Subject: [PATCH] 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 --- ChangeLog | 10 ++++++++++ Source/NSJSONSerialization.m | 2 +- Tests/base/NSJSONSerialization/json.m | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index d79692ffa..2a7350f09 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2014-01-07 Quentin Mathe + + * 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 * Source/NSOperation.m: Make sure current queue is set for threads diff --git a/Source/NSJSONSerialization.m b/Source/NSJSONSerialization.m index 17a9ebacc..83d6b299f 100644 --- a/Source/NSJSONSerialization.m +++ b/Source/NSJSONSerialization.m @@ -901,7 +901,7 @@ writeObject(id obj, NSMutableString *output, NSInteger tabs) } else { - [output appendFormat: @"%g", [(NSNumber*)obj doubleValue]]; + [output appendFormat: @"%.17g", [(NSNumber*)obj doubleValue]]; } } else if ([obj isKindOfClass: NSNullClass]) diff --git a/Tests/base/NSJSONSerialization/json.m b/Tests/base/NSJSONSerialization/json.m index 275eeca1f..6bbfe06a2 100644 --- a/Tests/base/NSJSONSerialization/json.m +++ b/Tests/base/NSJSONSerialization/json.m @@ -21,6 +21,7 @@ int main(void) \"emptyString\":\"\",\"emptyArray\":[],\"emptyObject\":{},\ \"IDs\": [116, 943, 234, 38793],\ \"escapeTest\": \"\\\"\\u0001\"\ + \"double\": 123.456789012254,\ }"; NSStringEncoding encs[] = {\ NSUTF8StringEncoding,\ @@ -48,7 +49,7 @@ int main(void) obj = tmp; } - PASS([obj count] == 6, + PASS([obj count] == 7, "Decoded dictionary had the right number of elements"); PASS([NSJSONSerialization isValidJSONObject: obj], "Can serialise deserialised JSON");