#50224 [Opn->Bgs]: json_encode() does not always encode a float as a float

From: Date: Thu, 19 Nov 2009 08:51:57 +0000
Subject: #50224 [Opn->Bgs]: json_encode() does not always encode a float as a float
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-144792@lists.php.net to get a copy of this message
 ID:          50224
 Updated by:  jani@php.net
 Reported By: christian dot lawrence at calorieking dot com
-Status:      Open
+Status:      Bogus
 Bug Type:    JSON related
 PHP Version: 5.2SVN-2009-11-19 (snap)
 New Comment:

There's is just "number" type in JSON for numbers. And as such, this is
working just like it should and PHP tries it's best at guessing what
type the numbers might be.


Previous Comments:
------------------------------------------------------------------------

[2009-11-19 05:45:10] christian dot lawrence at calorieking dot com

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 this bug report at http://bugs.php.net/?id=50224&edit=1



Thread (13 messages)

« previous php.bugs (#144792) next »