From: christian dot lawrence at calorieking dot com
Operating system:
PHP version: 5.2SVN-2009-11-19 (snap)
PHP Bug Type: JSON related
Bug description: json_encode() does not always encode a float as a float
Description:
------------
json_encode()-ing an integer when it is represented as floating point
number results in a change of type when json_decode() decodes the output.
Examples of such floating point numbers are: -123.0, -1.0, 0.0, 1.0, 123.0
Reproduce code:
---------------
<?php
function jsonRoundTrip($f) {
$e = json_encode($f);
$d = json_decode($e);
var_dump($f, $e, $d);
echo "\n";
}
jsonRoundTrip(12.3); // This is a float
jsonRoundTrip(12); // This is an integer
jsonRoundTrip(12.0); // This is an integer represented as a float
jsonRoundTrip(0.0); // This is an integer represented as a float
?>
Expected result:
----------------
float(12.3)
string(4) "12.3"
float(12.3)
int(12)
string(2) "12"
int(12)
float(12)
string(4) "12.0"
float(12)
float(0)
string(3) "0.0"
float(0)
Actual result:
--------------
float(12.3)
string(4) "12.3"
float(12.3)
int(12)
string(2) "12"
int(12)
float(12)
string(2) "12"
int(12)
float(0)
string(1) "0"
int(0)
--
Edit bug report at http://bugs.php.net/?id=50224&edit=1
--
Try a snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=50224&r=trysnapshot52
Try a snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=50224&r=trysnapshot53
Try a snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=50224&r=trysnapshot60
Fixed in SVN:
http://bugs.php.net/fix.php?id=50224&r=fixed
Fixed in SVN and need be documented:
http://bugs.php.net/fix.php?id=50224&r=needdocs
Fixed in release:
http://bugs.php.net/fix.php?id=50224&r=alreadyfixed
Need backtrace:
http://bugs.php.net/fix.php?id=50224&r=needtrace
Need Reproduce Script:
http://bugs.php.net/fix.php?id=50224&r=needscript
Try newer version:
http://bugs.php.net/fix.php?id=50224&r=oldversion
Not developer issue:
http://bugs.php.net/fix.php?id=50224&r=support
Expected behavior:
http://bugs.php.net/fix.php?id=50224&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=50224&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=50224&r=submittedtwice
register_globals:
http://bugs.php.net/fix.php?id=50224&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=50224&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=50224&r=dst
IIS Stability:
http://bugs.php.net/fix.php?id=50224&r=isapi
Install GNU Sed:
http://bugs.php.net/fix.php?id=50224&r=gnused
Floating point limitations:
http://bugs.php.net/fix.php?id=50224&r=float
No Zend Extensions:
http://bugs.php.net/fix.php?id=50224&r=nozend
MySQL Configuration Error:
http://bugs.php.net/fix.php?id=50224&r=mysqlcfg