7
Comments
  • 4
    No, you should rather explain what the data does when it gets serialized into JSON or where it gets processed.
  • 1
    @T1l3 and what if that place doesn't exist yet? Or if its a config file for a library you have no control over or access to.There are many cases i've wished I had comments.

    I once used a regex to find comments in JSON and put a wrapper around a file parsing function so that it stripped them out before parsing. It saved so much time and hassle across the team.
  • 0
    With json being used not just for communication between computers, but also for configs, I think it should support comments.

    It doesn't, so I prefer configs in yml or similar.

    @practiseSafeHex It would be difficult to create a safe regex. Example: Forward slashes in strings are often escaped, but it's not required, so a value could contain something looking like a comment.

    A good option is to use js instead of json where possible, and export the config object.
  • 1
    JSON does in a way. Obviously JSON is JavaScript Object Notation, and while it is also a popular data structure as of late, it's still a JavaScript thing, and JavaScript does have comments :)

    Doesn't really count, but it's worth something!
  • 0
    Another option is put comments a field that won't be deserialized. But either way it's not a good idea.
  • 1
    {
    "__comment__": "some booleans",
    "property1": true,
    "property2": false,
    "__comment__": "the end"
    }

    Is valid JSON
  • 0
    Go with YAML
  • 0
    @bittersweet Thanks for your comment. I had no clue what the OP (sorry @princebansal) was referring to until you mentioned 'I think it should support comments' :)
  • 0
    @Tobias But then there's a lot of code which enumerates json keys and crashes on your example 😉

    A preparser operation like JSmin is a better solution...

    Or, don't use JSON. It's a great format for data exchange between processes, but shit for writing configs.
Add Comment