News for hackers. Stuff that matters.
Re:Comments in JSON by avolcano on Friday August 02, 2013 @10:39AM (#6147150)
Can we all just agree, as a community, to add comment support to our JSON parsers? Hell, I'd do a PR on V8 if I knew C++.

It's ridiculous that I can't document notes on dependencies in my NPM package.json, or add a little reminder to my Sublime Text configuration as to why I set some value, because we're using JSON parsers that can't handle the concept of ignoring a line with a couple slashes prefixing it.

IMO - either we add comments to JSON, or we stop using it for hand-edited configuration.

Re:Comments in JSON by IanCal on Friday August 02, 2013 @10:48AM (#6147196)
> It's ridiculous that I can't document notes on dependencies in my NPM package.json, or add a little reminder to my Sublime Text configuration as to why I set some value

Why not have

    { "keyname" : "aldkjfhaldhfa"
      "keyname_comment" : "asdfjnad" }
If that's not enough, use something other than JSON. Adding comments will just result in it being valid in some parsers and not others.
Re:Comments in JSON by sixbrx on Friday August 02, 2013 @02:13PM (#6148642)
How do you know application programs won't barf if they see an unexpected key?
Re:Comments in JSON by sehrope on Friday August 02, 2013 @10:46AM (#6147189)
Or just use YAML[1]. It's a super set of JSON, includes comments, nicely formatted lists, and is (IMHO) much easier on the eyes.

[1]: http://en.wikipedia.org/wiki/Yaml

Re:Comments in JSON by RHSeeger on Friday August 02, 2013 @11:09AM (#6147367)
YAML is not a superset of JSON, it's a totally different format.
Re:Comments in JSON by TomasSedovic on Friday August 02, 2013 @11:17AM (#6147437)
According to the spec, YAML 1.2 is:

"The primary objective of this revision is to bring YAML into compliance with JSON as an official subset."

http://yaml.org/spec/1.2/spec.html

If I understand it correctly, the earlier versions were close but not 100% compatible.

Re:Comments in JSON by sehrope on Friday August 02, 2013 @11:20AM (#6147460)
YAML 1.2 is a super set of JSON. Check out the spec itself: http://www.yaml.org/spec/1.2/spec.html#id2759572
Re:Comments in JSON by bct on Friday August 02, 2013 @11:19AM (#6147456)
It's both.
Re:Comments in JSON by phpnode on Friday August 02, 2013 @10:43AM (#6147169)
Crockford's rationale for not supporting comments is that people use them to add meta data to the object (e.g. type annotations) which makes it hard to consume with different parsers.
Re:Comments in JSON by avolcano on Friday August 02, 2013 @10:45AM (#6147181)
Trusting the community to do the right thing is better than handicapping your users.

Regardless, of course, people add metadata to JSON already - there's zero reason you can't "_type": "int". It's a completely arbitrary reason.

Re:Comments in JSON by IanCal on Friday August 02, 2013 @10:52AM (#6147238)
> Regardless, of course, people add metadata to JSON already - there's zero reason you can't "_type": "int"

Which is fine, because it's in a format that everyone can parse. Adding

    //COMMAND: Extension(github:IanCal/preparser).parsethis
is the kind of things this forbids. Most peoples parsers would ignore this as a comment (if we have comments), but maybe some would do it in a special way. Either everyone ignores the comments in the parser (this is unlikely to carry on for long, someone will want to extend it) or nobody is allowed comments. That way everyone parses the same text.
Re:Comments in JSON by jerf on Friday August 02, 2013 @11:16AM (#6147424)
"Trusting the community to do the right thing is better than handicapping your users."

And the "community" in question had repeatedly and grossly demonstrated itself to be unworthy of such trust.

Crockford was not hypothesizing that this might happen, he'd seen it. Repeatedly. If you want to argue against it even so, fine, but bear in mind that is what you are arguing against, real pain that real people experienced, not mere possibilities.

Re:Comments in JSON by wvenable on Friday August 02, 2013 @12:10PM (#6147830)
JSON did support comments for a time and people were starting to use it for meta data.

The problem is that JSON is not meant to be used a configuration file format and just because it's really good for information exchange, doesn't mean it's good for configuration (and vice-versa). Configuration really requires comment support and information-exchange is better avoiding it. Two standards are needed.

Re:Comments in JSON by phpnode on Friday August 02, 2013 @10:48AM (#6147200)
right - but that is valid syntax! any json parser can understand that, and that's what he recommends doing instead. But if you're doing this in comments, you end up writing your own mini language to describe your annotations, and nothing else knows how to parse it. that should clearly be avoided.
Re:Comments in JSON by esailija on Friday August 02, 2013 @10:57AM (#6147275)
If JSON had comments, then of course any JSON parser could understand those comments just as well as they can currently understand "_type": "int". What am I missing?
Re:Comments in JSON by masklinn on Friday August 02, 2013 @11:00AM (#6147302)
That because they're comments specific JSON parsers could (and likely would) interpret processing instructions embedded in those comment to toggle behaviors on the fly. Crockford's fear (founded I think) was that comments would be used to "extend" json.
Re:Comments in JSON by esailija on Friday August 02, 2013 @11:05AM (#6147347)
Yes I get that. What I don't get is how "_processing_instruction": "whatever" is any different.
Re:Comments in JSON by phpnode on Friday August 02, 2013 @11:28AM (#6147531)
when you do

    {
        "_type": "int",
        "foo": "123"
    }
a JSON parser will always know how to represent that object. How you process that object is up to you.

However, when you write:

    {
        // @type int this is a comment
        "foo": "123"
    }
and you call JSON.parse(), what would you expect to get back? You can no longer represent it as a simple object, you need some way to access the comment, how do you do that? Moreover, whose responsibility is it to process the annotation in that comment? the parser's? Should you get back an integer rather than a string for obj.foo? how would you support different types of annotation? What happens if you're using parser A and your client uses parser B? Does parser B support all the annotations that parser A supports? If you need to modify a JSON structure, e.g. JSON decoding, adding a property and re-encoding, should the comments be preserved? ...

You can see that having comments introduces a whole host of other questions, ambiguity and would only make it harder for different platforms to share data. Avoiding this kind of cruft is why JSON is winning vs XML for most things these days.

Re:Comments in JSON by esailija on Friday August 02, 2013 @12:01PM (#6147759)
In the eyes of compliant parser (assuming JSON supported comments) it is just a comment like "_type": "int" is just a key-value pair.

However, when using ad hoc parser, then all bets are off what the result is in both cases again, not just the comment case. Regardless of comment support in JSON the same problem appears to exist.

Re:Comments in JSON by masklinn on Friday August 02, 2013 @10:52AM (#6147230)
And worse than metadata, processing instructions.
Re:Comments in JSON by roryokane on Friday August 02, 2013 @10:47AM (#6147191)
Sublime Text actually already supports '//' comments in its "JSON" configuration files, though it's non-standard. The comments are properly ignored, and syntax-highlighted. However, the comments (along with all other manual formatting) are lost if the file is programmatically edited, for example by changing the font size using the keyboard shortcuts.
Re:Comments in JSON by avolcano on Friday August 02, 2013 @10:50AM (#6147219)
Ah, thanks for the correction :)
Re:Comments in JSON by njharman on Friday August 02, 2013 @12:50PM (#6148077)
> we stop using it for hand-edited configuration.

Bing, Bing, Bing. We have a winnar!!!

XML sucks in large part not because of XML but because people used it for everything, everywhere in places it was highly ill-suited. Don't fuckup JSON the same way.

Re:Comments in JSON by sixbrx on Friday August 02, 2013 @02:15PM (#6148658)
I actually like xml about as much as anything else for complicated config files. I find the explicit (named) block closing tags to help readability.
[deleted]
Re:Comments in JSON by buro9 on Friday August 02, 2013 @10:41AM (#6147166)
TOML is nice for config files: https://github.com/mojombo/toml
Re:Comments in JSON by d0mine on Friday August 02, 2013 @10:45AM (#6147180)

  s/^#.*//g
or

  yaml.safe_load(json_file_with_comments)
Re:Comments in JSON by xsace on Friday August 02, 2013 @10:48AM (#6147203)
you can comment things out in sublime text configuration files