I recently added a proposal for Jason to add an option to allow decoding and encoding of NaNs (not a number) and ± infinity. This would allow decoding strings such as
{ "v1": NaN, "v2": Infinity }
So far the discussion was mainly between @OvermindDL1 and myself, and I would like to get more feedback on this.
Here is the gist of it.
Fact: The JSON spec doesn’t support it. Furthermore, it explicitely discourages it.
Numeric values that cannot be represented in the grammar below (such as Infinity and NaN) are not permitted. (RFC 7159)
Fact: Quite a few parsers out there have the option to deal with NaNs. Many of these even have this option enabled by default (which leads to quite a bit of “JSON” out there which is not spec compliant):
-
Python enables NaNs by default and provides an option to be spec complient
> import json > data = [float('nan'), float('inf'), float('-inf')] > data [nan, inf, -inf] > json.dumps(data) '[NaN, Infinity, -Infinity]'
-
GSON (JVM) provides an option to deal with NaNs but is spec compliant by default
-
Julia provides an option to deal with NaNs but is spec compliant by default
-
OCaml Yojson enables NaNs by default and offers a flag to be spec compliant
Fact: Various parsers do not allow for NaNs. This includes
- JavaScript
- Go
- Rust
- Currently all Elixir JSON libs
Question: What is an appropriate choice for a JSON parser?
- Be spec compliant and do not provide options for deviating from that.
- Allow for NaN parsing on the basis that JSON in the wild may be not spec compliant and the only way to properly deal with such strings is to write a JSON parser that can deal with these.
Looking forward to feedback.