-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix #79908: json_decode decodes negative zero as positive zero #7234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
While the JSON standard knows only numbers (i.e. it doesn't distinguish between int and float), and does not mention negative zero at all, PHP makes that distinction, so a negative zero should be parsed as such, even if the number is not otherwise designated as a float.
To be clear: PHP distinguishes between int and float; it does not parse |
I'm not sure this is the right way to go about it. I think it would make more sense to special case negative zero to JSON encode as I checked the Python and Ruby JSON implementations, and they both decode |
This reverts commit 7c03a10.
Encoding a negative zero as `-0` is likely to loose the sign when decoding (at least it does with `json_decode()`). Therefore, we encode it as if `JSON_PRESERVE_ZERO_FRACTION` was specified, i.e. as `-0.0`.
Might indeed be the better solution. New fix pushed. |
@cmb69 I'm not sure if I understand what is being fixed here. There has been explicitly RFC for this: https://wiki.php.net/rfc/json_preserve_fractional_part . Basically anyone who wants to preserve zero fraction (which logically includes -0.0 and 0.0) should use JSON_PRESERVE_ZERO_FRACTION. I think this makes it inconsistent with other numbers. Am I missing anything? |
@bukka, it is not about preserving the fraction, but about the sign. As it was, a negative 0 lost its sign after roundtrip: |
And that's kind of expected because it will be int so there's no difference between -0.0 and 0.0. If you do |
That was my first reaction to the bug report as well; however, this case is somewhat special, since without |
After reading the bug, I think it would make more sense to do
The first part is basically what V8 (NodeJS) does:
Although I don't see it as a bug so it should just target master. Unless there's something really broken (which is not in this case), changes in json encoding should not go to bug fixing releases as it might do more harm than good (e.g. might cause change of content after minor update which might not be expected). |
If it's not a bug, there's nothing to fix for me. I have reverted the commits, and sent a mail to the RMs to not forget to revert from PHP-7.4.22 and PHP-8.0.9 as well. |
This reverts commit 717f1ed, as requested by the author via #7234 (comment).
While the JSON standard knows only numbers (i.e. it doesn't distinguish
between int and float), and does not mention negative zero at all, PHP
makes that distinction, so a negative zero should be parsed as such,
even if the number is not otherwise designated as a float.