←back to thread

Fixing JSON

(www.tbray.org)
139 points robin_reala | 4 comments | | HN request time: 0.873s | source
Show context
outsidetheparty ◴[] No.12327880[source]
Shameful confession: when I was first introduced to JSON, I was convinced it would go nowhere. "XML already does everything JSON does! And there's no way to differentiate between nodes and attributes! And there are no namespaces! And no schemas! What's the point of JSON?" And a couple of years later I looked up from the tangled mess of XSLT I was working on to discover that the rest of the world had moved on.

JSON is just javascript, and it leaves out everything else. That's its entire reason for existing, and it caught on because that's all that 99.44% of anyone needed.

Timestamps you can add today, without changing the protocol; just put them in your data if you need them. So I'm not sure what he's even proposing there.

Schemas: OK, he doesn't like JSON Schema's anyOf. Fair enough. There's no real proposal here for how to fix it, so not much to say here.

Replacing commas with whitespace sounds to me like replacing a very minor irritant with a constant full-body rash. Stuff like his example of "IDs": [ 116 943 234 38793 ] would lead to far more confusion and errors than the occasional stray trailing comma.

So I guess I pretty much vote no on this one thanks for asking

replies(9): >>12327976 #>>12328071 #>>12328074 #>>12328283 #>>12329722 #>>12329776 #>>12330073 #>>12330517 #>>12334062 #
Normal_gaussian ◴[] No.12328074[source]
> Replacing commas with whitespace sounds to me like replacing a very minor irritant with a constant full-body rash

Both vivid and accurate.

In order to combat trailing commas I normally place the comma on the following line, e.g.

    { "time": "3 minutes past four"
    , "age": 229
    , "sex": "monoecious species"
    , "appearance": "Tree-like.  It's a tree."
    }

    uint8_t *data    // Yada
          , *buffer  // Ya
          ;

    var javascript
      , variable = 6
      , declarations = [ "This is taking too long", "Yep" ]
      , mix = [ "Flour", "Sugar", "Water" ]
      , types = { 'old'     : (d) => { return d < new Date }
                , 'new'     : (d) => { return d > new Date }
                , 'borrowed': (o) => { return false }
                , 'blue'    : (c) => { return true }
                }
      , regularly = new Date().toISOString()
      ;
    
With such a format there is only ever a problem deleting the first line, which I find is much much harder to do without also noticing what you've done to the larger statement.
replies(4): >>12328715 #>>12329576 #>>12330611 #>>12330704 #
majewsky ◴[] No.12330704[source]
I came across this formatting pattern in Haskell, but I still prefer trailing commas for one reason: I can trivially apply line-wise operations (e.g. sort or align) on the key-value pairs without breaking the syntax. When I sort your first snippet line-by-line, it becomes

  , "age": 229
  , "appearance": "Tree-like.  It's a tree."
  , "sex": "monoecious species"
  { "time": "3 minutes past four"
  }
and the syntax is broken. With trailing commas, the syntax always stays valid:

  {
    "age": 229,
    "appearance": "Tree-like.  It's a tree.",
    "sex": "monoecious species",
    "time": "3 minutes past four",
  }
replies(2): >>12330716 #>>12332601 #
saurik ◴[] No.12332601[source]
You are assuming that trailing commas are ignored as valid, which is true of JavaScript but not of JSON or of C (the other example).
replies(2): >>12334959 #>>12365642 #
tptacek ◴[] No.12365642[source]
Trailing commas are allowed in C!
replies(2): >>12367655 #>>12368010 #
1. mzs ◴[] No.12367655[source]
C89 v. C99 difference IIRC

edit: I was wrong: http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#initialize...

replies(1): >>12368022 #
2. saurik ◴[] No.12368022[source]
No: tptacek did not bother paying attention to the context; as far as I know "uint8_t * data, * buffer, ;" (spaces added after * to avoid italics) would never be valid.
replies(1): >>12368084 #
3. tptacek ◴[] No.12368084[source]
How is that the context? In the sense of the intersection between Javascript and JSON, trailing commas have pretty much always worked in C, which is something all of us who write code that generates C rely on, like a lot.
replies(1): >>12368525 #
4. mzs ◴[] No.12368525{3}[source]
I googled and trailing comma was allowed since C89, I was wrong. For code that generates code I always did something like this cause of that mistake:

  { a, b
  , c
  ...
  , z
  };