-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Supporting customization of float encoding in JSON #81022
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
Comments
Currently, there is only one argument which allows customization how float numbers are encoded in JSON: allow_nan. But this does not allow one to hook into the encoding of floating points really. The JSONEncoder is not called for float numbers. The motivation here is that we would like to encode NaN and Infinity values differently, instead of non-standard approach and instead of raising an exception. The "load" counterpart has "parse_float" which one can use to hook into parsing floats. I would suggest something similar, maybe "encode_float" which if provided would be used instead of the default. |
If no one is working on it, I'd like to give it a try. |
That would be awesome! BTW, just as an additional example, JavaScrpt's JSON.stringify encodes NaN and Infinity to null. By having a custom function I could for example try to match such implementation. |
I've sent a PR for it. |
I think this should be discussed on the mailing list or Discourse, to get use cases and needs. |
We just ran into this in Jupyter where we've removed a pre-processing step for data structures passed to json.dumps, which took care of this, but was expensive jupyter/jupyter_client#706 My expectation was that our |
@Lee-W (as an author of a child PR) The Discourse is https://discuss.python.org/. |
The existing thread https://discuss.python.org/t/harmonizing-the-json-serialization-of-non-finite-float-values-with-browsers-and-other-languages/20477 may be relevant. |
Is there any updates on this issue? the fact that the standard I think the solution proposed in the aforementioned thread can definitely be a decent solution! |
Yes, @encukou's second option in this message still seems like a good resolution to me. That is:
I'm not likely to have cycles to work on this any time soon, but would be willing to review a PR. |
See #115246. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: