bug-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug #35670] NSJSONSerialization writes unnecessary decimals after ints


From: Jens Alfke
Subject: [bug #35670] NSJSONSerialization writes unnecessary decimals after ints
Date: Wed, 29 Feb 2012 19:51:14 +0000
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.22 (KHTML, like Gecko) Chrome/19.0.1049.3 Safari/535.22

URL:
  <http://savannah.gnu.org/bugs/?35670>

                 Summary: NSJSONSerialization writes unnecessary decimals
after ints
                 Project: GNUstep
            Submitted by: snej
            Submitted on: Wed 29 Feb 2012 07:51:13 PM GMT
                Category: Base/Foundation
                Severity: 3 - Normal
              Item Group: Bug
                  Status: None
                 Privacy: Public
             Assigned to: None
             Open/Closed: Open
         Discussion Lock: Any

    _______________________________________________________

Details:

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])





    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/bugs/?35670>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]