4 ms·
These aren't foot-guns per se, but I can think of another handful of grievances I have with JSON: * JSON streaming is a bit of a mess. You can either do JSONL,
by woodruffw 5y ago
These aren't foot-guns per se, but I can think of another handful of grievances I have with JSON:
* JSON streaming is a bit of a mess. You can either do JSONL, or keep the entire document in memory at once. I usually end up going with JSONL.
* JSON itself doesn't permit trailing commas. I can measure the amount of time that I've wasted re-opening JSON files after accidentally adding a comma in days, not hours.
* JSON has weakly specified numbers. The specification itself defines the number type symbolically, as (essentially) `[0-9]+`. It's consequently possible (and common) for different parsers to behave differently on large numbers. YAML also, unfortunately, has this problem.
* Similarly: JSON doesn't clearly specify how parsers should behave in the presence of duplicate keys. More opportunity for confusion and bugs.
- secondcoming 5y ago2, 3 and 4 can be caught early with JSON schema.
- mirekrusin 5y agoNot really, json schema validation is applied after json parsing on already parsed json.
- afiori 5y agothe funny thing is that json doesn't even need commas, they essentially act as whitespace, any amount or no amount would make no difference in the meaning of the document. Arrays with hole are a JS-only feature
- the_jeremy 5y ago> json doesn't even need commas JSON is defined by the spec. The people who wrote the spec think otherwise[0]. [0]: https://www.json.org/json-en.html https://www.json.org/json-en.html > Arrays with hole are a JS-only feature. There are other langauges that allow arrays with missing elements.
- afiori 5y agoJSON requires commas, but does not need them, semantically they are treated like whitespace The document > {1:2 3:[4 5]} can only be "commafied" to > {1:2, 3:[4, 5]} > There are other langauges that allow arrays with missing elements. But javascript is the only one that gave JSON the JS in its name
- jrockway 5y agoRunning prettier (https://prettier.io https://prettier.io) on each save will fix trailing commas for you. If you accidentally have one, it will just sneakily remove it and turn your document into one that is valid.
- spullara 5y agoYou can parse JSON in a streaming fashion with many libraries. You just don't know at the beginning if it is going to be valid or not.
- lkschubert8 5y agoAnd the flip side of that with YAML is you can stream it, but you don't know once you've gotten to the end if it was the whole document without some user defined checksum mechanism.
- spullara 5y agoRan into a great bug with the INI format which has the same issue. The application would read the config file on modification but if you just wrote over the file it would sometimes read the config before the file was fully written. Have to use a temp file and move it rather than just edit it.
- gray_-_wolf 5y agoIt's possible to have document start and end markers in yaml: --- foo: 1 ... Your application can mandate usage of these. But yeah, not ideal.
- mdaniel 5y ago> Your application can mandate usage of these I believe that's only true if one were to load YAML via the "SAX"-style per-event stream, and not the "object materialization" that normal apps use (aka `yaml.load_all` or JAX-B objects) since in those more data-object centric views, where would one put the processing events for those markers? I also originally expected `yaml.parse(...)` to eat them as it does for comments and extraneous whitespace, but no, it does in fact return dedicated stream events for them, so TIL
- xorcist 5y agoHow someone could have decided on a subset of javascript and not include comments is beyond me.
- zosima 5y agoIt may have been a good or bad decision. But comments were intentionally left out of JSON to avoid obvious ways to sneak in parsing directives and thus incompatibilities between different JSON-parsers.
- deepsun 5y agoYet incompatibilities persist from day 1: big integers, duplicate keys, keys order. On the other hand, XML allows comments, yet I've never seen XML parsers incompatibilities.
- singron 5y agoNot exactly an incompatibility, but my mind jumped to issues like this: https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SAML%20Injection/README.md#xml-comment-handling https://github.com/swisskyrepo/PayloadsAllTheThings/blob/mas... <NameID>user@user.com<!--XMLCOMMENT-->.evil.com</NameID> Some parsers will take just the first text element ("user@user.com"), and others will concatenate the text elements ("user@user.com.evil.com").
- bmn__ 5y ago> Some parsers will take just the first text element Those are not in compliance with the relevant spec. We need to treat them as damage and confront them on the technical and social level.
- Too 5y agoIf I had a penny every time someone tried to parse xml using a regex, if that classifies as a parser. Those are 100% incompatible with everything else. Easiest way to demonstrate how wrong that is, is to throw in a comment in the example document ;)