vim-json icon indicating copy to clipboard operation
vim-json copied to clipboard

Add optional support for comments

Open TheLocehiliosan opened this issue 8 years ago • 9 comments

While not officially part of the JSON standard, there are many parsers which will accept comments.

This change allows users to set an option which recognizes comments as comments instead of an error.

TheLocehiliosan avatar Sep 25 '15 19:09 TheLocehiliosan

Hi, it would be great to add support for comments that do not break the JSON syntax. IE:

{
   "_comment": "comment text goes here...",
   "glossary": {
      "title": "example glossary",
      "GlossDiv": {
         "title": "S",
....
....

See: http://stackoverflow.com/questions/244777/can-i-use-comments-inside-a-json-file#comment14790821_244858

epcim avatar Oct 16 '15 15:10 epcim

The problem with the "_comment": "Comment here", style of comment is that it adds superfluous data which your programs later may have to deal with. For example, you might want to add a comment in a data structure which is expecting a hash of hashes. If one of the values happens to be a string instead of a hash, you could have problems. So you have to "filter out" the "_comment" stuff. :/

It's also problematic to put a comment on the same line as data, or in the middle of a data structure when "adding extra comment data".

As for syntax-highlighting that type of comment, it's probably possible, but what happens if a key "_comment" points to a value which is a larger data structure? Should that whole structure be highlighted as a comment? Maybe it only highlights the rest of that line as a comment? I might see if I can work up something useful that is turned on by setting a variable with the "comment" key to be used. Something like let g:vim_json_comment_key=_comment

TheLocehiliosan avatar Oct 17 '15 01:10 TheLocehiliosan

Hi @elzr , any possibility of this PR getting merged? I also find it quite useful, and being optional, it does not break 'normal' JSON behaviour.

Thanks

jcristovao avatar Oct 01 '16 06:10 jcristovao

Hey @elzr , I agree that this seems perfectly reasonable. Even Douglas Crockford said that using json for configuration files and including comments is reasonable.

Suppose you are using JSON to keep configuration files, which you would like to annotate. Go ahead and insert all the comments you like. Then pipe it through JSMin before handing it to your JSON parser.

bmartin-pan avatar Jan 26 '17 12:01 bmartin-pan

I'm also a fan of this pull request and hope it gets merged in some form.

One suggestion: Support b:vim_json_comments instead of or in addition to g:vim_json_comments for enabling/disabling comment support. That way comment support can be enabled on a per-buffer basis (e.g. using autocmd to enable comments only for JSON files known to allow comments) rather than requiring it to be enabled for all JSON files.

kevinoid avatar May 17 '19 00:05 kevinoid

Since this PR doesn't seem to be going anywhere, I decided to package the changes as vim-jsonc using the jsonc filetype to make it easy to use until support is added to vim-json in some form.

kevinoid avatar Aug 11 '19 04:08 kevinoid

How would one change all json files to be treated as jsonc? It would be great if this could just be merged in some way to make it easier to use.

ell1e avatar Mar 04 '21 22:03 ell1e

How would one change all json files to be treated as jsonc? It would be great if this could just be merged in some way to make it easier to use.

@ell1e I believe this should do the trick (if you didn't already find the solution):

autocmd BufNewFile,BufRead *.json setl ft=jsonc

willeccles avatar Jun 14 '21 17:06 willeccles

:+1:

IzhakJakov avatar Jun 30 '21 00:06 IzhakJakov