Re: json_decode/encode should return full precision values by default
| From: | Anthony Ferrara | Date: | Mon, 27 Jul 2015 21:54:01 +0000 |
| Subject: | Re: json_decode/encode should return full precision values by default | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-87330@lists.php.net to get a copy of this message | ||
Yasuo,
On Sun, Jul 26, 2015 at 4:20 PM, Yasuo Ohgaki <yohgaki@ohgaki.net> wrote:
> Hi Jakub,
>
> On Mon, Jul 27, 2015 at 3:32 AM, Jakub Zelenka <bukka@php.net> wrote:
>
>> I don't think that this is a bug. Your example is also completely
>> unrelated to json because the place when the value is rounded is var_dump
>> where it's based on ini precision. You would get the same values with
>> json_encode but it's only because it uses the same ini ( precision ).
>> Basically it has nothing to do with json_decode.
>>
>
> OK. Better example.
>
> [yohgaki@dev PHP-master]$ ./php-bin
> <?php
> $j = '{ "v": 0.1234567890123456789 }';
> var_dump(json_encode(json_decode($j)));
> ini_set('precision', 20);
> var_dump(json_encode(json_decode($j)));
> ?>
>
>
> string(22) "{"v":0.12345678901235}"
> string(28) "{"v":0.12345678901234567737}"
I think you missed the point. Parsing isn't dependent upon precision
setting. Only dumping: http://3v4l.org/48VSt
$j = '{ "v": 0.1234567890123456789 }';
$d1 = json_decode($j);
ini_set('precision', 20);
$d2 = json_decode($j);
var_dump($d1, $d2);
//object(stdClass)#1 (1) { ["v"]=> float(0.12345678901234567737) }
//object(stdClass)#2 (1) { ["v"]=> float(0.12345678901234567737) }
Meaning that it's parsed correctly.
There is no bug here.
Anthony