JSON.stringify(JSON.parse(foo))
Presumably you'd say that it should return a plain JSON object without comments, right? However, with the current implementation this will return a JSON document that is identical to the input, modulo whitespace. This makes it easy to write tools that can, for example, increment the version number in a nodejs package.json file programatically. Doing this in a world where comments exist becomes extremely awkward or at the very least annoying, because either you have to write your own JSON parser that preserves comments, or you have to simply discard the comments when you're writing the file.If you go with the first option, you'll inevitably run into a scenario where you have a document like this contrived example:
{
// this is the first ever version!
"version": "1.0.0"
}
And your tool goes off and increments the version number, so you end up with: {
// this is the first ever version!
"version": "2.0.0"
}
and now your comment is a lie.If you go with the second option, you destroy all comments whenever you write data back to the file, so they can be deemed temporary at best.
What benefit do you actually get from having comments in either scenario?
Supporting comments makes JSON more convenient to use in more situations, and it doesn't prevent you from doing anything you can do now.
And this may even encourage people into separating the data payload from the metadata, who knows?
Besides, JSON has already become a common format for config files. Config files that don't allow comments are truly a step backward.
I don't see how this is a "no true scotsman". Maybe a non-true "no true scotsman"?
> If comments were added to the spec then normal tools would support them just fine.
Plain comments, sure. Comments with custom annotations in them, not so much.
> Besides, JSON has already become a common format for config files. Config files that don't allow comments are truly a step backward.
Maybe this wasn't such a good idea in the first place?