Hacker News new | ask | show | jobs
by adamc 1172 days ago
JSON is great, but I surely wish it supported comments. That's the nature of its failings: too minimal.
5 comments

That depends on what you want it to be. For a data interchange format, having no comments is arguably a strength. For a config file format, having no comments is a big weakness.
Just do

  {
    "someSetting": true
    "comment": "TODO change to false when ready"
  }
Though really text-based protobufs are better for config.
Problems are that some tools will rewrite the file and reorder the "comment" away from what it's meant to comment on. Also might complain the "comment" item isn't expected there. I seem to remember package.json suffering from both of these under the control of npm.
Yeah it's definitely a limited solution, and I prefer that JSON be kept simple that way. Many package.json-adjacent configs like the Babel stuff can be in .js files, giving you comments and everything.
Many people use text format protobufs for config. It vaguely resembles yaml, and the schema is enforced by virtue of requiring a message definition in order to parse it.
This always bothered me. A coworker once suggested using fields ending in 'notes' to put in comments but I never really warmed up to that.
I have heard that too but it’s just a terrible idea.
Luckily a good number of parsers support extensions to JSON like comments and trailing comma's.
Comments are simple to parse, but preserving them on the dump is complex. I guess they were sacrificed for the simplicity.
They were excluded so people wouldn't use them to insert meta-processing instructions into the JSON doc.

In reality people insert those meta-processing instructions in other ways.

That’s a good point. It would be hard to read the JSON, modify it and then write back with comments.

But you still should have the option to at least ignore them while reading. That would make JSON config files so much better to work with.

You can use JSONC, which is JOSN with C style comments.