> Coming from this direction, the addition of commas feels like an evil plan to have more syntax errors, with no obvious benefit
It helps to learn your history before you criticise something and claim it is useless.
JSON is the way it is mainly because it is just JavaScript and that meant that every browser in the world already supported it before JSON was even invented. It is THE reason why it is as popular as it is.
I think the spirit is forbidding creative developers from using the comments to include active content. I know people who if struggling with datetime encoding, would add a "// DATEFMT: YYYY-D-M" comment and be on their merry way, making it incompatible with all other JSON parsers.
What might be nice is to add a prefix to key name that indicates 'this is a 'internal document reference', skip this unless quests to parser to not ignore. Would allow for describing encoding method(s) used. date format comments being very helpful. Technically, can do by just dropping by key name.
Why comment on a format vs. adding an associated "key""format" with format information. aka
date : 25.02.04
date-fmt : YY.DD.MM
I really with the semicolon weren't optional. Its "I'll take my best guess" parsing is maddening.
One that keeps killing me is:
return
(complicated-multi-line-computation)
Since it can put a semicolon at the end of the first line, it does. Which means the return value is undefined, and the rest of it is just some code that it never actually reaches. A linter will fix that, but I find the usual fix a bit ugly:
There are a few minor, uncommon edge cases, probably only encountered by minimisers, where automatic semicolon insertion may cause unintentional behaviour, so semicolons are required. One of the specs; https://262.ecma-international.org/7.0/#sec-rules-of-automat... (link from an earlier comment of like) spells it out in detail.
Relying on the inherent JavaScript compatibility hack has caused as many problems as it has solved. Plus, the additional of trailing commas made a mess of things once more, forcing developers to let go of the direct compatibility with many versions of JavaScript engines and redirecting them to specialised JSON APIs instead.
The easy solution would be to go the JavaScript route and make commas optional, like JavaScript does with semicolons. You could even introduce vague and easily forgotten rules about commas being inserted between oneliner dictionaries.
I think the question "why" is easily answered, but "why should it still" is more difficult.
The problem is not commas, but the fact the specification does not tell you MUST, in a conforming parser, handle extra commas at the end. While commas are not very useful in this case, they have some value because:
1. They help humans parse the JSON object: we are helped by structure.
2. They add redundancy of information and allow for simpler spotting of syntax errors.
What JSON really needs is to extend its syntax to allow strings that are prefixed length, without the need of a binary protocol. "foo": 6="foobar". Otherwise JSON forces you to parse byte by byte, which is extremely inefficient for large amounts of data.
I don't find commas at the end of line adding any more readability than avoiding them, except for the oneliner counter case.
JavaScript has optional separators (semicolons) with some rules to avoid ambiguity. Not everyone like it (I use semicolons in JS unnecessarily because I can't be bothered to learn the three edge cases they're not optional). I think there's something to be said for "commas are optional except in oneliners".
Languages like Go describe structs without commas. It took me some getting used to, but it's really not more or less readable in my opinion.
If readability was the only criteria perhaps YAML deserves to win. For some data XML is more readable, its complexity is the bigger problem.
People went all in with things like XSLT and namespaces, Java became the "language that transforms XML into stack traces" for no good reason and complex XML monstrosities were considered normal so when something simple came along it was a much needed breath of fresh air (we were even prepared to overlook its lack of comments). If it wasn't JSON it would have been something else, I'm sure.
YAML seems easier to read, but for me it's much harder to actually read properly since so many things are ambiguous. JSON is very explicit - you always know the data type you're looking at. YAML has a bunch of special cases that you have to remember, and these frequently lead to bugs.
The best example are boolean values. With JSON, you have `true` or `false`. With YAML you have all of these: y|Y|yes|Yes|YES|n|N|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF
We are trained from a very young age, as a rule, to see the comma. The semicolon, not so much. Semicolons are unusual: they are not part of story telling; they are from a forgotten grammar which is hardly used.
Pooh had wandered into the Hundred Acre Wood, and was standing in front of what had once been Owl's House. It didn't look at all like a house now; it looked like a tree which had been blown down; and as soon as a house looks like that, it is time you tried to find another one.
What do you mean, not part of story telling? There are 64 semicolons in The House at Pooh Corner; and by the way, nearly all of them are followed by conjunctions.
I get the argument, but I can't imagine a double-blind study of children or adults of any age that would find they see a semicolon versus a comma at a different rate. If anything, the reverse argument works; the semicolon is more obvious because it's so unusual.
I kinda disagree with the conclusion. Punctuation is important to humans, it makes it more parsable/readable. I just accept that in a multi-line context they are not so useful, because the newline acts like a kind of punctuation. So I could make them optional and provide some recommendations: use them in a single line, and don't use them when a newline follows.
These JSON versus YAML versus whatever discussions seem to mainly be arguments from people hand-coding it in grey ASCII with no tools. IMO the ASCII itself just needs to be machine-readable, because humans have visualization tools to parse it and give a pretty view. I'm not Rain Man.
Haskell culture tends to do this kind of stuff for records, FWIW. Of course, in this case, it might even make sense to render this in a way such as the following:
Because as another person already said, the first line isn't movable, and the last line wasn't either. The first key-value pair does look a tad strange because it doesn't have anything before the key, but nevertheless, now changing things is even easier.
Admittedly, it would look nicer if we could have trailing commas, though. Oh well, we have YAML for extra readable record syntax, even with how horrible YAML can get in reality.
In configuration languages such as HCL, having to account for the fact that there will not be a comma for the last element becomes unnecessarily difficult.
It helps to learn your history before you criticise something and claim it is useless.
JSON is the way it is mainly because it is just JavaScript and that meant that every browser in the world already supported it before JSON was even invented. It is THE reason why it is as popular as it is.