php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79908 json_decode decodes negative zero as positive zero
Submitted: 2020-07-28 13:02 UTC Modified: 2020-07-29 11:45 UTC
From: stefan at HDmeteo dot com Assigned:
Status: Re-Opened Package: JSON related
PHP Version: 7.4.8 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
48 - 10 = ?
Subscribe to this entry?

 
 [2020-07-28 13:02 UTC] stefan at HDmeteo dot com
Description:
------------
While php_encode stores a negative zero float as "-0" and preserves its sign, the subsequent php_decode loses the sign and decodes "-0" as "0".

Test script:
---------------
$array = [round(-0.1), round(0.1)]; // [-0,0]
print_r($array);                    // Array ( [0] => -0 [1] => 0 ) 
$json = json_encode($array, true);  // "[-0,0]"
$array = json_decode($json);        // [0,0]
print_r($array);                    // Array ( [0] => 0 [1] => 0 )




Expected result:
----------------
Array ( [0] => -0 [1] => 0 )

Actual result:
--------------
Array ( [0] => 0 [1] => 0 )

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-28 16:15 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-07-28 16:15 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

If you want to preserve the floating point number (there are no
integers with value -0), you have to pass JSON_PRESERVE_ZERO_FRACTION;
see <https://3v4l.org/1ZRm8>.
 [2020-07-29 10:57 UTC] stefan at HDmeteo dot com
while JSON_PRESERVE_ZERO_FRACTION solves the problem superficially, the behaviour of json_encode/json_decode is not logically clean.

There are 2 clean solutions:

1. Fix json_encode: 
do not output "-0" as there is no negative integer 0.

encode (float) (-0.0) as "0" without JSON_PRESERVE_ZERO_FRACTION
encode (float) (-0.0) as "-0.0" with JSON_PRESERVE_ZERO_FRACTION

2. Fix json_decode: 
interprete "-0" as negative float 0.0 as there is no negative integer
0.

decode "0" as (int) 0
decode "-0" as (float) (-0.0)
 [2020-07-29 11:45 UTC] cmb@php.net
-Status: Not a bug +Status: Re-Opened -Assigned To: cmb +Assigned To:
 [2020-07-29 11:45 UTC] cmb@php.net
Well, I think it makes sense to check what other JSON
implementations do in this case.
 [2020-07-30 09:32 UTC] stefan at HDmeteo dot com
JavaScript does the decoding as suggested in my solution 2 (decode -0 as float -0.0 or as string "-0"):

https://jsfiddle.net/:

input:
JSON.parse("-0")

output:
-0
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Fri Oct 30 05:02:34 2020 UTC