-
-
Notifications
You must be signed in to change notification settings - Fork 22.9k
Handle NaN and Infinity in JSON stringify function #108837
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
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code looks good to me. This makes sense as a best-effort attempt to serialize JSON while being as close as possible to the input.
5211b03
to
cc25266
Compare
Why not serialize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are apparently quite a few JSON parsers that use strings or 'keywords' to encode NaN or infinity even though it's not JSON compliant.
Python has a parameter for this (allow_nan
). I think we should copy this idea, as I can imagine some people want this technically uncompliant JSON behavior.
Two problems with the string representation:
For NaN, Anyway, if we want this functionality, it must be opt-in (as already proposed above, so, good). However, this would mean we need to add a parameter to |
The JSON specification does not support NaN or Infinity values. However, it does support scientific notation. https://www.json.org/json-en.html
Without this PR, trying to export JSON containing NaN or Infinity will result in invalid JSON, which cannot be parsed back by Godot, nor can it be parsed by web browsers.
With this PR, trying to export JSON containing NaN and Infinity will result in valid JSON, with Infinity being replaced with extremely large numbers, and NaN being replaced with null. These large values are read in as Infinity both by Godot and by web browsers, and this seems to be a common trick in the JSON ecosystem. See graphite-project/graphite-web#813
The unit tests added in this PR include examples of round-trip parsing, and the same tests fail without the other changes.