6 ms·
I'd be happy about a JSON spec that allows for comments...
by mschuster91 9y ago
I'd be happy about a JSON spec that allows for comments...
- fvdessen 9y agoYou should have a look at JSON5 http://json5.org/ http://json5.org/
- rspeer 9y agoThe fact that it has no support for Unicode at all -- neither literal nor escaped -- is just tragic.
- NoahTheDuke 9y agoHm? The spec[1] has reference to Unicode all over the place. [1]: https://json5.github.io/json5-spec/ https://json5.github.io/json5-spec/
- rspeer 9y agoIf it's supported, then they should update json5.org, which says "TODO: Unicode characters and escape sequences aren’t yet supported in this implementation."
- jrimbault 9y agoThe reason why comments aren't allowed anymore in the spec: https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaGSr https://plus.google.com/+DouglasCrockfordEsq/posts/RK8qyGVaG... And a conversation about it: https://news.ycombinator.com/item?id=3912149 https://news.ycombinator.com/item?id=3912149
- coldtea 9y ago>The reason why comments aren't allowed anymore in the spec Is BS.
- kroltan 9y agoExactly. { "__directive__": "phpstyle_stringly_typed", "boolean": "true" }
- BillinghamJ 9y agoNope, because that is still completely valid JSON. Sure it's kind of a shitty choice of structure, but every JSON parser on the planet will be able to read it successfully. If you choose to layer some extra logic/rules on top of the actual parsing, that's fine - that would be entirely within the intended purpose of JSON. Comments are FAR more likely to be used more like "preprocessor directives" to influence actual parsing rules, changing the syntax. Whereas your example conforms perfectly to the syntax, leaving parsing as-is and ensuring the logic is handled at a higher level.
- xg15 9y agoBut if you're already changing the syntax, why do you worry about JSON interop at all? Just invent your own language. If you do worry about JSON interop, then JSON-with-parsing-directives-disguised-as-comments would be valid JSON as well - just treat the directives as comments and ignore them. The resulting tree would probably not reflect the data that the author ment to encode (because you ignored the directives) - but neither would the tree from the example above.
- mseebach 9y agoIt seems a bit like a baby-and-bathwater situation, honestly. It seems feasible to put in the standard something like that comments must be stripped out in the first pass and may never influence the interpretation of the non-commented JSON. A parser-implementation might choose to ignore that, but then it wouldn't be a standards-compliant JSON parser, in the same way it wouldn't be if it brazenly ignored any of the other sections of the standard.
- foobarchu 9y agoYou could, but it probably would be ignored. He was completely justified in making his decision, since the exact problem was worried about is rife in XML.
- ChrisSD 9y agoPersonally I've never found that to be an issue when using JSON as a data interchange format. The only place I'd like to have comments is in configuration files and in that case there are less verbose formats that do allow for comments.
- viraptor 9y agoI never really understood json for config files. It's not that readable. It's great for data exchange and relatively human-readable representation, but if you ask people to actually write and maintain it, why not yaml / toml / ...?
- vidarh 9y agoYAML suffers from significant whitespace, which makes me for one refuse to ever consider it. Personally I consider YAML less human writable than JSON for that reason. TOML is tolerable, but all my preferred languages already have JSON parsers I'm familiar with, but while they may have TOML parsers, I'd have to go looking for onne.
- jghn 9y agoHOCON[1] is a pretty reasonable compromise, IMO. I've really only seen it used by Scala and Scala-y Java, which makes sense considering its lineage. [1] https://en.wikipedia.org/wiki/HOCON https://en.wikipedia.org/wiki/HOCON
- krapp 9y agoJSON is readable enough. The format is simple - no inline functions, no circular references, just maps or arrays of key-value pairs. This makes it well suited for describing what a config file should, which is an initial application state, without much needless abstraction. Config isn't meant to be code, and config files shouldn't be updated often, so a config file format doesn't need a great deal of expressiveness. Dealing with quoting keys and commas in JSON isn't more or less of a hassle than dealing with significant whitespace or whatever pecadillos those other formats have. Although personally I really like Lua tables for config because they have an even simpler structure than JSON and you can add comments. I have to resist the tempation to turn Lua config files into applications, though.
- tlocke 9y agoWhat about Zish? It's like JSON but with comments, a timestamp type, a decimal type and a bytes type: https://github.com/tlocke/zish https://github.com/tlocke/zish (as the author, I'm biased)
- billpg 9y agoMy unsolicited comments. I really want to see a good JSON-done-right out there and your spec is maybe 90% there. I'd rather only /* */ comments were allowed. By allowing //, you've now got two varieties of white-space the parser needs to be aware of. Because of this, I can't take multiple lines of ZISH and join them together into a single line. (Something both JSON and XML can do.) What is a "line"? CR, LF, CRLF, LFCR, FF, VT, U+0085, U+2028, U+2029? Am I required to treat ZISH files as "binary"? Could I convert all of those end-of-line bytes into my OS's preferred form without losing data? The notation of ending a line with a \ also prevents joining multiple lines into a single line. Maybe formalize "string"+"string" as an explicit string joining notation? (Or if not +, some other character to notate string concatenation.) Given you have the decimal type, is float necessary? Values with a "e" are still expressed with decimal digits. Is there a limit to numeric types? What if my ZISH file contained a several hundred 9s in a row? Sets seems unnecessary given you already have lists. You can take a list and treat it as a set after parsing with no loss of data. I very much like the addition of timestamps and binary without needing a string. This is my big pet peeve of JSON. Your examples include uses of null and true, but your spec doesn't define those values. (Or false for that matter.) The description of map doesn't specify if the order of keys is significant. Is {"a":"b", "c":"d"} the same as {"c":"d", "a":"b"} ?
- tlocke 9y agoThis is quite brilliant, thanks billpg. I'm also amazingly grateful that someone's taken an interest in Zish :-) These questions will all require some thought to properly understand and respond to, with no-doubt some changes to the spec. So I've opened an issue and put the comment at: https://github.com/tlocke/zish/issues/2 https://github.com/tlocke/zish/issues/2
- Flenser 9y ago
- SiVal 9y agoI'd rather have trailing commas, so every line can have the same format. Other people choose JSON for their config file format, and I don't have a say in it. I just have to do extra editing each time I reorder the lines or add a new one, because too bad. And I have to manually edit JSON when I'm writing code to handle other people's JSON APIs to test my code. I'll probably never not have to manually edit JSON, so JSON's insistence that using the same format for your final line as for all the others won't ever be allowed will remain a never ending, unnecessary nuisance in the spirit of JavaScript itself: the "too bad, too late now" school of design. Except that in this case, even JavaScript corrected that particular nuisance and allows trailing commas.
- dtech 9y agoIf you want a JSON-ish config format, I'd recommend to check out HOCON [1], basically a superset of JSON that fixes all he things that make it annoying for config files. [1] https://github.com/lightbend/config https://github.com/lightbend/config
- eptcyka 9y agoIf we're proposing supersets of json, why wouldn't one just use yaml?
- zimbatm 9y agoAll because IE6 wasn't supporting trailing commas at the time when JSON was written. Shortly after that we discovered that it was a bad idea to eval() JSON so the whole reason for browser compatibility became moot. But who knows, maybe that is what was needed at the time to make JSON popular.
- IgorPartola 9y agoIf the config is in the form of: { "file": "/etc/foo", "daemon": true } Couldn't you add comments like so, all within the existing spec? { "file": "/etc/foo", "_daemon_comment": "default is false", "daemon": true }
- olavk 9y agoGoes to show that there is no perfect format for all use cases. The lack of comments is a good thing for interoperability in machine-to-machine communication. But it a bad thing for hand-written configuration files.
- dtech 9y agoWhy are no comments good for interoperability? Numerous formats can contain comments and I've never seen someone having a problem parsing them out, especially since they usually cannot be nested and/or allow you to skip until the next newline. I personally dislike it the most when you want to provide a commented example of your data format, vanilla JSON won't allow you to do that.
- drderidder 9y ago{ "_comments": [ "Hello." ] }
- Mizza 9y agoYou probably don't want comments sent on the wire, but if you like JSON as a config format but want it to me more human-tolerant, you can try HJSON: https://hjson.org/ https://hjson.org/ I like it more than any other config format, really.
- philsnow 9y agothis has many of the misfeatures of yaml, like ambiguity of values. { a: 5 } is that "5" or 5? with JSON it's unambiguous.