-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
gh-98306: Support JSON encoding of NaNs and infinities as null #115246
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
Note: Strictly speaking, this is a backwards incompatible change: |
I can't comment on the C implementation as I'm simply unfamiliar with that part of the codebase, but one semantic change I'd suggest is to use "as_null"/"convert_to_null" instead of "null". In my mind, Apart from that, I'd really like to avoid using stringified versions of reserved keywords in the code, even if they come from other languages. I'm sure anyone who was unfortunate enough to debug serialization errors can attest to that. |
One more thing: Can we type-hint the |
SGTM. I've updated the PR and the description.
Python's own std. lib. source doesn't use type hints, as a rule; this would need to be a change in typeshed. |
|
Adding DO-NOT-MERGE label, pending response to python/steering-council#244. |
API suggestion: Do not use a str literal for the new flag value, use a new singleton Use of a Something like this: class _Singleton:
def __bool__(self):
raise RuntimeError(f"Not usable in boolean context.")
AS_NULL = _Singleton() and just check for that using |
Slightly, but not in a way that matters a whole lot. Though if you use AS_NULL instead of a literal as mentioned above, this concern goes away. People will no longer be passing a str and the minimum version supporting this API is clear by the existence of AS_NULL or not. Instead of the code behaving differently based on version when the literal is passed in. |
@gpshead I like the suggestion in principle, and it would definitely alleviate the backwards compatibility concerns to the point where an SC exception wouldn't be necessary. The practical question: how to implement this in a way that the new I guess there are two, independent, parts to this:
The |
@mdickinson the re module appears to just use integer flags. You may also want to look at the |
Closing. I'm not going to have the bandwidth to push this through in the forseeable future. (But if anyone wants to build on what's already here, please feel free to do so.) |
This PR:
allow_nan="as_null"
in JSON encoding (json.dump
,json.dumps
,JSONEncoder.encode
). This offers an encoding mode that automatically converts floating-point infinities and NaNs to JSONnull
s. This matches the ECMAScript specification forJSON.stringify
, so makes interoperability between JavaScript and Python somewhat easier.allow_nan
, to allow space for future expansion of the protocol if necessaryNote that strictly speaking, this is a backwards incompatible change: allow_nan="null" was valid before (since the string "null" is truthy), and this PR changes its meaning. The likelihood of actual breakage from this change seems negligible. Nevertheless, per PEP 387 we would need to request a Steering Council exception before this could be merged.
📚 Documentation preview 📚: https://cpython-previews--115246.org.readthedocs.build/