1

We have special files that contain JSON data mixed with # comments.

I figured I need to enhance Code's json.settings file with:

"files.associations": {
    "*.ourextension": "jsonc"
}

but then I discovered that jsconc is about JSON data with // comments.

Is there a convenient way to get VS code to accept # comments in JSON data?

Edit: VS code recognizes the jsconc language, it gives me this error message:

error-message

And it also accepts // comments:

comments

adding the // got me a green first line, and now the second line gets the first error (because starting with #).

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • Been digging around. No luck. Suggestion, why not modify its source code? It should be simply changing characters. Or write an extension and use [this](https://stackoverflow.com/a/35788355/12188799). – Javier Silva Ortíz Oct 25 '19 at 07:14
  • @JavierSilvaOrtíz I rethought your first idea, and got myself two small helper scripts. Turned that into a self-non-answer... – GhostCat Oct 25 '19 at 08:33

2 Answers2

1

If you use the Change Language mode command (or click on the language indicator on the status bar) you can select "jsonc JSON with Comments".

I think this is only auto-detected when the extension is .jsonc.

NB. JSON with comments uses JavaScript style single line comments: from a \\, outside a string literal, to the end of line.

To support some different comment indicator would require a new language mode (and an extension to add it).

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Wrong, please see my updates. The file type is correctly determined, as I explicitly told VS code to map my file extension to jsonc. – GhostCat Oct 25 '19 at 07:08
  • @GhostCat Expanded: a different comment style requires a different language – Richard Oct 25 '19 at 07:22
1

A distinct non-answer: it might be possible to add a such a new language definition, but it would require quite a bit of work. I also had a quick look if I could simply change the corresponding config json file for jsonc that ships with VS code, but that file is rather complex, and would probably be overridden with the next VS code update.

Thus a straight forward workaround. Two scripts to replace one command style with the other:

#!/bin/sh

# a helper script that turns all # into //
# with the syntax that works for sed on MAC OS
for file in "$@"
do
    sed -i '' -e 's,#,//,g' $file
done

Not exactly convenient, but fast and robust, given our specific requirements.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • A good reminder of the often niceness of shell scripts. Simple, elegant. As for convenient, maybe not in your specific scenario, but what if that code ever needs to go outside your company? – Javier Silva Ortíz Oct 25 '19 at 14:36