4 ms·
{ ... "comment": "this is a comment", ... }
by fctorial 7y ago
{
...
"comment": "this is a comment",
...
}
- switch007 7y agoIs that a comment...or a key called "comment"? :p Surely "#__comment__!11!!1#" would reduce the confusion !
- skywhopper 7y agoYeah, that doesn’t scale very well...
- fctorial 7y agoPut cursor after "t" in "comment" and slap your keyboard.
- laumars 7y agoComments are meant to stand apart from code, which that doesn't. Also that would only work if your JSON doesn't have a schema or the importing tool is lazy (ie doesn't validate properties against an expected structure).
- RabbiPires 7y agoUser editable files should _not_ be in JSON. TOML, YAML or heck, even INI (which is already in use by systemd anyway!) is better.
- jolmg 7y agoOr you know, you can just do your language equivalent of: grep -Pv '^\s*#' to remove comments, passing the output of that to the JSON parser. The extension could be modified to .cjson, .json.commented, or something. Good thing JSON doesn't permit literal newlines in strings. Any valid JSON should be able to go through that comment filter without getting modified.
- catalogia 7y agoThat's nice and all, but you'll still have trouble reinserting those comments when writing JSON. And if you don't have that, you have a config file which can only be read, not written. And if you accept that, then what was the point of using JSON in the first place?
- jolmg 7y ago> you'll still have trouble reinserting those comments when writing JSON. Sure. I wouldn't even try. > And if you don't have that, you have a config file which can only be read, not written. Just like most config files. > And if you accept that, then what was the point of using JSON in the first place? To not define your own format and be able to use ready-made parsers. It's not like I'm advocating for the use of JSON over other choices like YAML. I'm just replying to a comment about how to add comments to a JSON with a better alternative. If someone wants to use JSON over other choices for whatever reason and the only thing holding them back is the lack of comments, then this is a valid way to add comments, I believe. If you use a string property, you can't split your comment among multiple lines. You could perhaps do an array of strings or something, but I think adding a simple commenting syntax like this is neater. EDIT: Fixed double negative in penultimate paragraph.
- kbumsik 7y agoThat is not a "proper" comment though.