Your JSON Is Lying to You

36 points by gkoos


spc476

Parsing JSON is a minefield, from about a decade ago.

Verfeuil

What behaviors are strictly related to json and not javascript ? Yes, json originated from javascript-land, but a lot of those problems look like they originates from the use of javascript, and not json per-se.

fleebee

The mistake is treating a deliberately tiny interchange format as a lossless snapshot of JavaScript state. A JavaScript object and its JSON representation should be treated as different data models connected by an explicit conversion.

I think this is the most important takeaway here.

Many developers use JSON to move state over expecting a verbatim copy, but unfortunately there's a myriad of footguns scattered all over the place, and it works fine until it doesn't.

antonmedv

This is why https://maml.dev exists)

noirscape

Basically the only real problem here that isn't just JavaScripts JSON parser behaving in a surprising way is that JSON can't represent all float values. NaN is a real float value, but serializing/deserializing it is nothing short of a headache. (Date and time objects I would regard as complicated enough to just make them explicit ISO format serialization if you need timezone data or to use Unix timestamps, everything to do with time is complicated.)

An extension to JSON is unfortunately impossible/a nightmare to deploy, but actual support for NaN would be very helpful. As-is, you're stuck in the nightmare of "manually swap all your NaNs with null before serializing" or "extend the parser to special case NaNs to be nulls", which is both repeated code, can slow down the (de)serializer and 99/100 instances when using JSON (as a message format), you really don't care about the distinction between NaN and null anyways. It's a case of "the serializer complains about this for the extremely remote situation where it's an issue".

One of the niceties of orjson (in python) is that it does that for you (and also handles the NaN/NaT values of numpy in the same way with an option flag, because numpy operations can just return those values when mercury is in retrograde - in other words, almost completely randomly and without logic).

stig

That first issue rang a bell... I fixed a very similar issue in SBJson 13 years ago.