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

Group
 
 

bug #35670: NSJSONSerialization writes unnecessary decimals after ints

Submitter:  Jens Alfke <snej>
Submitted:  Wed 29 Feb 2012 07:51:13 PM UTC
   
 
Category:  Base/Foundation Severity:  3 - Normal
Item Group:  Bug Status:  Fixed
Privacy:  Public Assigned to:  None
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment Rich Markup
   

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

Thanks ... I applied that fix.

Richard Frith-Macdonald <CaS>
Group 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>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

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

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2012-03-01 CaS Open/ClosedOpen Closed
    2012-03-01 CaS StatusNone Fixed

    Back to the top

    Powered by Savane 3.13-d3ae.
    Corresponding source code