Why does JSON have commas?
simonsafar.com
simonsafar.com
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.
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.
const foo = someFn()
(foo as Bar).doSomething()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: return (
(complicated multi line computation)
) var myObject = eval(httpResponseBody)
Of course, this is vulnerable to all kinds of issues so we got JSON parser, and later JSON became part of the Web API.> ... before you criticise something and claim it is useless.
These 2 statements feel quite far apart.
(Sadly, Crockford's post about this reasoning was on Google Plus and is no longer online.)
{copyright comment : "JSON doesn't require/enforce specific comments is a good thing" }
{comments : "JSON parser is not an 'comment' editor! }
Don't know whtat the google plus article covered, but Crockfor's comments : https://www.linkedin.com/posts/douglas-crockford-724600109_j...
Why comment on a format vs. adding an associated "key""format" with format information. aka date : 25.02.04 date-fmt : YY.DD.MM
There is nothing in the JSON spec that prohibits pre/post non-json parsing search / replace 'end of line' marker with one or more commas.
JSON spec just tells one what the JSON parser expects, not what the end user needs/wants. (GPT-JSON not withstanding)
Just because we can throw away redundancy doesn’t mean we should
https://clojure.org/guides/learn/hashed_colls#_creating_a_li...
*it is one way of initializing a dictionary. Literal way has a closer to JSON form.
EDIT: Oh you seem to be talking about ObjC it seems! I did not see the nil. Literal way is still more akin to JSON though.
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.
To be fair, dictionary one-liners might be a tiny bit less easy to read:
{"key1": "value1" "key2": "value2"}
Not just a bit less easy, a lot less easy
Is "key1" array of ["value1", "key2"] ? or string "value1"? We can't now until we find ':'.
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.
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.
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.
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
By that logic one could argue the whole JSON format is bad because of all the key name redundancy.
is written
{ :interests<编程 reading travel>, }
in raku literals, quite a lot less wiggling of the pinky than the authors “maximal” proposal
yeah raku : perl without the line noise
I don't even mind indentation for nesting, but this
key: value
vs key:value
is EVIL.https://github.com/paulmooreparks/Xfer/tree/master/ParksComp...
It keeps the message readable and it can be re-assembled on the server. That is if MessagePack or CBOR are not an option.
{"foo": "bar"
,"baz": "hehe"
,"yes": "aligned"
,"better": "this way"}I’ll agree with that, since they’re all vertically aligned.
> Also you can put the last brace on its own line
You can always put the first and last brace on their own line in most languages whether the commas are at the front or end.
{
"foo": "bar"
, "baz": "hehe"
, "yes": "aligned"
, "better": "this way"
}
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.
Allow linebreaks to insert implied commas
It's an adamant No from me.