Why is Python not JSON Standard (rfc7159) Compliant by default?

Viewed 153

The Python3 standard lib for JSON, https://docs.python.org/3/library/json.html, documents the following (emphasis mine):

If allow_nan is false (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification. If allow_nan is true, their JavaScript equivalents (NaN, Infinity, -Infinity) will be used.

Further down on the encoder:

If allow_nan is true (the default), then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

While it is good they allow this as an option for those who are capable of deliberately parsing NaN, why doesn't Python follow the JSON spec as the default?

1 Answers

The answer is in the same sentence: because it's consistent with other encoders and decoders that don't follow the standard.

JSON was not standardized until 2013, and the decision to require out-of-range values to raise an error was probably made despite the fact that common practice was to generate special values.

There are de jure standards (like EMCA-404/RFC 8259/ISO/IEC 21778:2017), and there are de facto standards (which is what everyone does in the absence of a real standard or for backwards compatibility to pre-standard practice).

Related