php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40503 json_encode integer conversion is inconsistent with PHP
Submitted: 2007-02-16 00:47 UTC Modified: 2007-02-18 16:55 UTC
From: mchenryc at gmail dot com Assigned: iliaa (profile)
Status: Closed Package: Output Control
PHP Version: 5CVS-2007-02-16 (snap) OS: Windows XP
Private report: No CVE-ID: None
 [2007-02-16 00:47 UTC] mchenryc at gmail dot com
Description:
------------
json_encode converts integers to string in a different way than PHP's integer to string conversion (at least on win32). PHP appears to either handle unsigned values, or use more bits to encode integers, where as json_encode converts them to floats, causing rounding errors.


Reproduce code:
---------------
function show_eq($x,$y) {
  echo "$x ". ($x==$y ? "==" : "!=") ." $y\n";
}

$value = 0x7FFFFFFF; #2147483647;
show_eq("$value", json_encode($value));
$value++;
show_eq("$value", json_encode($value));


Expected result:
----------------
2147483647 == 2147483647
2147483648 == 2147483648

Which shows both functions handling unsigned (or "long") integers.

Or:

2147483647 == 2147483647
2147483650 == 2147483650

Which shows both functions converting to a float with less precision.


Actual result:
--------------
2147483647 == 2147483647
2147483648 != 2147483650

My code makes use of the "long" or "unsigned" (not sure what's actually happening) ability of PHP for unix timestamps, which I would dearly love to see stay.  The real issue is that the two functions behave in different ways, and should not.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-18 16:55 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Nov 23 07:01:29 2024 UTC