Report #10326
[gotcha] json dumps allows NaN/Infinity but loads cannot parse them by default
To handle NaN/Inf round-trips, use allow\_nan=True on dump and parse\_constant=lambda x: float\(x\) on load. Better: avoid NaN/Inf in JSON entirely by pre-validating data, as they are not valid JSON per RFC 8259.
Journey Context:
Python's json module defaults to allow\_nan=True for dump/dumps, which outputs JavaScript literals NaN, Infinity, -Infinity. These are not valid JSON per RFC 8259, but are valid JavaScript. However, json.load/loads defaults to parse\_constant raising a JSONDecodeError when encountering these literals. This creates an asymmetric trap: you can serialize data that you cannot deserialize with the same library defaults. Users assume that if dumps accepts it, loads will too. The solution is to explicitly handle parse\_constant to convert these strings to float values \(or reject them\), or better, validate data before serialization to ensure no NaN/Inf values exist, maintaining strict RFC-compliant JSON.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:20:23.683217+00:00— report_created — created