News for hackers. Stuff that matters.
Re:Fixing JSON by majewsky on Sunday August 21, 2016 @08:37AM (#12330704)
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",
  }
Re:Fixing JSON by tome on Sunday August 21, 2016 @08:41AM (#12330716)
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"
    }
Re:Fixing JSON by azinman2 on Sunday August 21, 2016 @02:51PM (#12332171)
Is the first comma legal in Json or JavaScript? I'd have thought that'd be an error.
Re:Fixing JSON by reaktivo on Monday August 22, 2016 @05:25AM (#12335068)
Valid but introduces undefined as the first item of the array
Re:Fixing JSON by saurik on Sunday August 21, 2016 @04:30PM (#12332601)
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).
Re:Fixing JSON by majewsky on Monday August 22, 2016 @05:00AM (#12334959)
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.
Re:Fixing JSON by tptacek on Friday August 26, 2016 @08:16AM (#12365642)
Trailing commas are allowed in C!
Re:Fixing JSON by mzs on Friday August 26, 2016 @01:09PM (#12367655)
C89 v. C99 difference IIRC

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

Re:Fixing JSON by saurik on Friday August 26, 2016 @01:59PM (#12368022)
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.
Re:Fixing JSON by tptacek on Friday August 26, 2016 @02:07PM (#12368084)
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.
Re:Fixing JSON by mzs on Friday August 26, 2016 @03:06PM (#12368525)
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
  };
Re:Fixing JSON by saurik on Friday August 26, 2016 @01:57PM (#12368010)
The contextual example from C was "uint8_t * data, * buffer;" (spaces added to avoid italics), and no: a trailing comma is absolutely not allowed.
Re:Fixing JSON by tptacek on Friday August 26, 2016 @02:08PM (#12368092)
Replied here, to the first time I saw you write this:

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