News for hackers. Stuff that matters.
Re:Fixing JSON by Normal_gaussian on Saturday August 20, 2016 @03:56PM (#12328074)
> 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.
Re:Fixing JSON by yepperino on Saturday August 20, 2016 @07:18PM (#12328715)
> 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.

Re:Fixing JSON by philovivero on Sunday August 21, 2016 @01:39AM (#12329567)
Ah, so you're the guy whose code I have to re-reformat back to the proper format.
Re:Fixing JSON by yepperino on Sunday August 21, 2016 @09:31AM (#12330860)
I use industry standard IOCCC formatting rules. Doesn't everyone?

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

Re:Fixing JSON by ketralnis on Saturday August 20, 2016 @08:41PM (#12328949)
If you're having to do it on every job, maybe you're the unusual one
Re:Fixing JSON by yepperino on Sunday August 21, 2016 @12:13AM (#12329325)
Weirder still - why do I follow him from job to job?
Re:Fixing JSON by ngrilly on Sunday August 21, 2016 @11:41AM (#12331279)
I like where this is going :-)
Re:Fixing JSON by Normal_gaussian on Sunday August 21, 2016 @08:14AM (#12330642)
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.

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

Re:Fixing JSON by stevewilhelm on Sunday August 21, 2016 @01:41AM (#12329576)
I have seen this approach also used when constructing complex SELECT FROM and WHERE clauses in MySQL
Re:Fixing JSON by truth_sentinell on Sunday August 21, 2016 @08:05AM (#12330611)
That is horrible from a human understanding perspective
Re:Fixing JSON by Normal_gaussian on Sunday August 21, 2016 @08:12AM (#12330633)
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.

Re:Fixing JSON by paulddraper on Sunday August 21, 2016 @02:24PM (#12332050)
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?