Bug #79908 [Com]: json_decode decodes negative zero as positive zero

From: Date: Thu, 18 Feb 2021 11:46:19 +0000
Subject: Bug #79908 [Com]: json_decode decodes negative zero as positive zero
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-232246@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79908&edit=1 ID: 79908 Comment by: stasiukaitis dot saulius at gmail dot com Reported by: stefan at HDmeteo dot com Summary: json_decode decodes negative zero as positive zero Status: Re-Opened Type: Bug Package: JSON related PHP Version: 7.4.8 Block user comment: N Private report: N New Comment: 3rd party library stoped working after upgrade to PHP 7.4 This is because it compares data before and after encoding which becomes different. https://github.com/psecio/jwt/blob/master/src/Psecio/Jwt/Jwt.php#L157 Previous Comments: ------------------------------------------------------------------------ [2020-07-30 09:32:52] 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 ------------------------------------------------------------------------ [2020-07-29 11:45:28] cmb@php.net Well, I think it makes sense to check what other JSON implementations do in this case. ------------------------------------------------------------------------ [2020-07-29 10:57:07] 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-28 16:15:24] 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-28 13:02:21] 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 ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79908&edit=1

« previous php.bugs (#232246) next »