Most active commenters
  • Normal_gaussian(3)
  • yepperino(3)
  • saurik(3)
  • tptacek(3)

←back to thread

Fixing JSON

(www.tbray.org)
139 points robin_reala | 25 comments | | HN request time: 1.878s | source | bottom
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 #
1. 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 #
2. yepperino ◴[] No.12328715[source]
> In order to combat trailing commas I normally place the comma on the following line

Ah okay, so you're that guy whose code I have to reformat in every new job.

replies(3): >>12328949 #>>12329567 #>>12330642 #
3. ketralnis ◴[] No.12328949[source]
If you're having to do it on every job, maybe you're the unusual one
replies(1): >>12329325 #
4. yepperino ◴[] No.12329325{3}[source]
Weirder still - why do I follow him from job to job?
replies(1): >>12331279 #
5. philovivero ◴[] No.12329567[source]
Ah, so you're the guy whose code I have to re-reformat back to the proper format.
replies(1): >>12330860 #
6. stevewilhelm ◴[] No.12329576[source]
I have seen this approach also used when constructing complex SELECT FROM and WHERE clauses in MySQL
7. truth_sentinell ◴[] No.12330611[source]
That is horrible from a human understanding perspective
replies(2): >>12330633 #>>12332050 #
8. Normal_gaussian ◴[] No.12330633[source]
It makes perfect sense to me.

Which to be honest is what matters. However I would imagine that a lot of the problem grokking it is due to not being used to it.

9. Normal_gaussian ◴[] No.12330642[source]
I highly recommend having a couple of reformatting scripts around - its a great way to find logical errors quickly.

Of course don't commit reformats where it diverges from the codebase.

10. 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 #
11. tome ◴[] No.12330716[source]
With prefix commas it also always stays valid

    {
    , "age": 229
    , "appearance": "Tree-like.  It's a tree."
    , "sex": "monoecious species"
    , "time": "3 minutes past four"
    }
replies(1): >>12332171 #
12. yepperino ◴[] No.12330860{3}[source]
I use industry standard IOCCC formatting rules. Doesn't everyone?

http://www.ioccc.org/2015/endoh1/prog.c

13. ngrilly ◴[] No.12331279{4}[source]
I like where this is going :-)
14. paulddraper ◴[] No.12332050[source]
Why?

It looks easier for me; I'm far more likely to notice a missing comma.

But I'm sure you must have a valid point, if you would elucidate?

15. azinman2 ◴[] No.12332171{3}[source]
Is the first comma legal in Json or JavaScript? I'd have thought that'd be an error.
replies(1): >>12335068 #
16. 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 #
17. majewsky ◴[] No.12334959{3}[source]
Yes, I'm aware of this being an error in JSON (which is precisely the point of the discussion). I wasn't aware that it's a problem in C. Maybe gcc is more lenient here.
18. reaktivo ◴[] No.12335068{4}[source]
Valid but introduces undefined as the first item of the array
19. tptacek ◴[] No.12365642{3}[source]
Trailing commas are allowed in C!
replies(2): >>12367655 #>>12368010 #
20. mzs ◴[] No.12367655{4}[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 #
21. saurik ◴[] No.12368010{4}[source]
The contextual example from C was "uint8_t * data, * buffer;" (spaces added to avoid italics), and no: a trailing comma is absolutely not allowed.
replies(1): >>12368092 #
22. saurik ◴[] No.12368022{5}[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 #
23. tptacek ◴[] No.12368084{6}[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 #
24. tptacek ◴[] No.12368092{5}[source]
Replied here, to the first time I saw you write this:

https://news.ycombinator.com/item?id=12368022

25. mzs ◴[] No.12368525{7}[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
  };