arnomi

arnomi

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.

Showing Posts 1 to 2

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.

xxoo

xxoo

You can use jsex in javascript projects. which is an extended JSON serializer/deserializer. it supports NaN, Infinity and many other data types.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
achempion
I’ve been using Emacs as my main code editor for more than a two years. It’s a custom build version although I’ve tried doom emacs and sp...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
jtormey
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
georgeguimaraes
Just published claude-code-elixir, a plugin marketplace for Claude Code with Elixir support. These are the plugins I’ve been using for my...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews