Sublime Forum

Why does Sublime Text 3 allow comments in JSON configuration files?

#1

Using comments in JSON configuration files in Sublime Text can make JSON objects unable to be decoded.Here is my story:
I newly installed SublimeREPL plugin in my Sublime Text 3. Soon I discovered it ran Python2.7 instead of 3.5 in default, so I added my own Python3.5 configuration files according to SublimeREPL Docs to make it support Python3.5.

My Packages/SublimeREPL/config/Python3.5/Main.sublime-menu JSON config file looks like this:

[
{
“id”: “tools”,
“children”:
[{
“caption”: “SublimeREPL”,
“mnemonic”: “R”,
“id”: “SublimeREPL”,
“children”:
[
{“caption”: “Python3.5”,
“id”: “Python3.5”,

         "children":[
            {"command": "repl_open",
             "caption": "Python3.5",
             "id": "repl_python3.5",
             "mnemonic": "P",
             "args": {
                "type": "subprocess",
                "encoding": "utf8",
                "cmd": ["python3", "-i", "-u"],
                "cwd": "$file_path",
                "syntax": "Packages/Python/Python.tmLanguage",
                "external_id": "python3",
                "extend_env": {"PYTHONIOENCODING": "utf-8"}
                }
            },
            // run files
            {"command": "repl_open",
             "caption": "Python3.5 - RUN current file",
             "id": "repl_python3.5_run",
             "mnemonic": "R",
             "args": {
                "type": "subprocess",
                "encoding": "utf8",
                "cmd": ["python3", "-u", "$file_basename"],
                "cwd": "$file_path",
                "syntax": "Packages/Python/Python.tmLanguage",
                "external_id": "python3",
                "extend_env": {"PYTHONIOENCODING": "utf-8"}
                }
            }
        ]}
    ]
}]

}
]

Note there is a comment // run files in this file. This config works fine from the menu bar tools->SublimeREPL->Python3.5. However,when I tried to bind the F5 key with repl_python3.5_run to make it easier access to 3.5,the following exception was thrown in the console:

After I removed the // run files comment. The F5 key works fine.It’s exactly the comment that cause the problem. Sublime Text uses JSON as config files,lots of config files come with // style comments. As we all know, comments were removed from JSON by design.

Then how can sublime text allow comments in config files,is it using a pipe? If it is, how can my key binding fail?

0 Likes

#2

Sublime Text’s configuration files deviate from the JSON spec in a few ways, such as allowing trailing commas as well as comments. This is done to make it more reasonable to edit them by hand. To parse them programmatically, the best option is to use sublime.decode_value(string), which will use the same parser that Sublime Text itself uses.

1 Like

#3

I think Sublime’s JSON format can be parsed by jsonC parsers

JSONC is extended JSON spec which includes comments (JSONC stands for JSON with Comments)

1 Like

#4

@jps it should be straight forward to add a new JSON syntax that doesn’t support comments, and rename the current one to JSONC.

0 Likes

#5

I’m not sure that Sublime’s extended JSON format is identical to JSONC. I do like the idea of separating “true JSON” from “Sublime extended JSON”, but rather than tie it to a third-party standard, it might be best to just call it “JSON (Sublime)” or something and let third-party packages offer and maintain exact JSONC functionality.

0 Likes

#6

I don’t see any difference between JSONC and sublime configs but i agree that the new syntax should be given a native name.

0 Likes

#7

See also https://github.com/sublimehq/Packages/issues/285.

@AmjadHD can you point me to a specification of this “JSONC” format? As I mention in that issue, I looked around a bit for one and couldn’t find any.

0 Likes

#8

Strangely, i couldn’t find one either, but I think JSONC was made by Microsoft and uses it for VSCode’s configurations files, this is their jsonc parser https://github.com/microsoft/node-jsonc-parser/blob/master, if you’re interested.
But you shouldn’t need the spec it’s just JSON with javascript comment’s.

0 Likes

#9

Well, ST additionally accepts trailing commas, but that doesn’t matter for syntax highlighting at least.

0 Likes