4 ms·
Unless the config file uses JSON as a format. Then no comments for you!
by pohl 15y ago
Unless the config file uses JSON as a format. Then no comments for you!
- amalcon 15y agoAck! You're right, but why is this the case? JavaScript obviously has comments, so someone must have made the decision to deliberately leave them out. What possible reason could there be for that?
- FooBarWidget 15y agoJSON is meant to be a serialization language, not a config file language. Therefore it strips out all the things that it doesn't need. If you want a config file language then use YAML which is a much better candidate.
- amalcon 15y agoYes, YAML is very nice. Importantly, it reduces to key:value for the 90% of config files that don't require any hierarchy while still allowing hierarchy if you need it. I'm not sure I buy the serialization argument, though: it's a human-readable serialization format. One of the big advantages of human-readable formats is that you can craft data objects by hand (for automated tests, initial bootstrapping, etc). Comments are very helpful for those situations.
- JonnieCache 15y agoIt also has inheritance-like features that make it really suited to config files. JSON really isn't a good idea for config files when we have YAML.
- orangecat 15y agoI've done something like this: {"height": 100, "width": 150", "__comment__": "This is a comment"} Of course depending on the implementation details this may keep the comment string in memory. I agree JSON should have native comments, as well as optional trailing commas.
- alavrik 15y agoI don't think JSON as a _data interchange format_ should have comments. Key factors contributing to JSON's popularity and great practical value is simplicity and the fact that JSON is a final specification (i.e. no versioning, no extensions, no backward compatibility requirements). Although JSON is human-readable, it wasn't specifically designed for human interaction. There are other projects and languages that address this specific topic. For example, Yaml and Piq. (Piq is the one that I'm working on as a part of the Piqi project -- http://piqi.org http://piqi.org).
- ootachi 15y agoYou can simulate comments this way: { "width": "comment goes here", "width": 50, "height": 30 } Yes, it's a terrible hack, but it works in every JSON parser I've seen.