bugGNUstep - Bugs: bug #35670, NSJSONSerialization writes...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #35670: NSJSONSerialization writes unnecessary decimals after ints

Submitted by:  Jens Alfke <snej>
Submitted on:  Wed 29 Feb 2012 07:51:13 PM UTC  
 
Category: Base/FoundationSeverity: 3 - Normal
Item Group: BugStatus: Fixed
Privacy: PublicAssigned to: None
Open/Closed: Closed

Thu 01 Mar 2012 09:31:51 AM UTC, comment #1:

Thanks ... I applied that fix.

Richard Frith-Macdonald <CaS>
Project Member
Wed 29 Feb 2012 07:51:13 PM UTC, original submission:

When NSJSONSerialization encodes an integer, it writes it out in floating-point format with five decimals, i.e. "123.00000" instead of "123".
Not a big deal, but it's unattractive and makes the JSON output longer than it needs to be.

ANALYSIS
The writeObject() function, at NSJSONSerialization.m:832, outputs any NSNumber by calling:
[output appendFormat: @"%f", [obj doubleValue]];
The default formatting of "%f" is five decimal places, apparently.
Changing the format string to "%g" suppresses unnecessary decimals and causes integers to be written as integers. (See patch below.)

CONFIGURATION
URL: http://svn.gna.org/svn/gnustep/libs/base/trunk/Source
Repository Root: http://svn.gna.org/svn/gnustep
Repository UUID: 72102866-910b-0410-8b05-ffd578937521
Revision: 34837
My OS is Ubuntu 11 (current according to software update).
I'm compiling with Clang 3.1 (trunk 151546).

PATCH

Index: NSJSONSerialization.m
===================================================================
--- NSJSONSerialization.m (revision 34837)
+++ NSJSONSerialization.m (working copy)
@@ -829,7 +829,7 @@
}
else
{
- [output appendFormat: @"%f", [obj doubleValue]];
+ [output appendFormat: @"%g", [obj doubleValue]];
}
}
else if ([obj isKindOfClass: NSNullClass])

Jens Alfke <snej>

 

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by CaS (Posted a comment)
  • -unavailable- added by snej (Submitted the item)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 2 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Thu 01 Mar 2012 09:31:51 AM UTCCaSOpen/ClosedOpen=>Closed
    Thu 01 Mar 2012 09:31:50 AM UTCCaSStatusNone=>Fixed

    Back to the top


    Powered by Savane 3.1-cleanup1