arnomi

arnomi

JSON extensions such as NaN handling

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?

  1. Be spec compliant and do not provide options for deviating from that.
  2. 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.

Most Liked

alco

alco

What would be the expected behaviour in Elixir when parsing a JSON document with NaNs? BEAM doesn’t support NaN nor infinities, so I would expect to get back an error saying the input can’t bee decoded.

But if we’re talking about an extension to the parser, then it should probably define its own representation for such invalid values, e.g. parse NaNs into :NaN, and user code will have to depend on this particular representation. Moreover, I assume there’s not universal representation for encoding such value in different languages (it’s not part of the spec and there’s no separate spec for handling NaNs and infinities).

These observations lead me to the conclusion that it won’t be sufficient to add an option to a JSON library to enable support for non-conformant values because there may be different representations for such values in the wild.

A proper solution from a JSON library perspective would be to allow the user to hook into the parser and extend it in the desired way. So I see it as an extension mechanism rather than a single configuration option.

Where Next?

Popular in Discussions Top

jswny
I would like to better understand what the advantages/disadvantages of umbrella applications are compared to structuring your app as as s...
New
Rustixir
Hi everyone, im working on find best language/framework/system for high concurrency, high performance and stable performance after wor...
New
mmmrrr
Just saw that dhh announced https://hotwire.dev/ Is it just me or is this essentially live view? :smiley: Although I like the “iFrame-e...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19652 166
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
ben-pr-p
In general I’ve been sticking to this community style guide GitHub - christopheradams/elixir_style_guide: A community driven style guide ...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
MarioFlach
Hello, I want to share a project I’ve been working on for a while: https://github.com/almightycouch/gitgud Background Some time ago I ...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement