From:             lucas at facebook dot com
Operating system: linux, osx
PHP version:      5CVS-2007-06-02 (CVS)
PHP Bug Type:     JSON related
Bug description:  json_encode double conversion is inconsistent with PHP

Description:
------------
json_encode is ignoring PHP integer precision and losing precision on
large doubles. The patch below changes json_enode double to match the same
conversion done internally in _convert_to_string.

This is related to http://bugs.php.net/bug.php?id=40503 and reverts the
patch applied there. Tested against these use cases as well.

Reproduce code:
---------------
json_encode(123456789.12345)

Expected result:
----------------
123456789.12345 = 123456789.12345

Actual result:
--------------
123456789.12345 = 123456789



Index: json.c
===================================================================
RCS file: /repository/php-src/ext/json/json.c,v
retrieving revision 1.9.2.15
diff -u -r1.9.2.15 json.c
--- json.c      25 May 2007 13:24:50 -0000      1.9.2.15
+++ json.c      2 Jun 2007 03:41:47 -0000
@@ -354,15 +354,11 @@
                 double dbl = Z_DVAL_P(val);
 
                 if (!zend_isinf(dbl) && !zend_isnan(dbl)) {
-                       len = spprintf(&d, 0, "%.9g", dbl);
-                       if (d) {
-                               if (dbl > LONG_MAX && !memchr(d, '.',
len)) {
-                                       smart_str_append_unsigned(buf,
(unsigned long)Z_DVAL_P(val));
-                               } else {
-                                       smart_str_appendl(buf, d, len);
-                               }
-                               efree(d);
-                       }
+                    len = spprintf(&d, 0, "%.*G", (int) EG(precision),
dbl);
+                    if (d) {
+                        smart_str_appendl(buf, d, len);
+                        efree(d);
+                    }

-- 
Edit bug report at http://bugs.php.net/?id=41567&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=41567&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=41567&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=41567&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=41567&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=41567&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=41567&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=41567&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=41567&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=41567&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=41567&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=41567&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=41567&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=41567&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=41567&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=41567&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=41567&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=41567&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=41567&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=41567&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=41567&r=mysqlcfg

Reply via email to