为什么 Sublime Text 3 允许在 JSON 配置文件中添加注释?

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

在 Sublime Text 的 JSON 配置文件中使用注释可以使 JSON 对象无法解码。这是我的故事。

我在我的Sublime Text 3中新安装了SublimeREPL插件。很快我发现它运行 Python2.7而不是默认的3.5,所以我添加了我自己的Python3.5根据 SublimeREPL Docs 配置文件使其支持 Python3.5.

我的 Packages/SublimeREPL/config/Python3.5/Main.sublime-menu JSON 配置文件如下所示:

[
 {
    "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"}
                    }
                }
            ]}
        ]
    }]
}]

请注意,此文件中有注释 // 运行 files。此配置在菜单栏工具->SublimeREPL->Python3.5 中运行良好。但是,当我尝试将 F5 键与 repl_python3.5_run 绑定以更轻松地访问 3.5 时,控制台中抛出了以下异常:

Traceback (most recent call last):

  File "./python3.3/json/decoder.py", line 367, in raw_decode
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "/opt/sublime_text/sublime_plugin.py", line 551, in run_
    return self.run(**args)

File "/home/ubuntu/.config/sublime-text-3/Packages/SublimeREPL/run_existing_command.py", line 32, in run
    json_cmd = self._find_cmd(id, path)

File "/home/ubuntu/.config/sublime-text-3/Packages/SublimeREPL/run_existing_command.py", line 41, in _find_cmd
    return self._find_cmd_in_file(id, file)

 File "/home/ubuntu/.config/sublime-text-3/Packages/SublimeREPL/run_existing_command.py", line 53, in _find_cmd_in_file
    data = json.loads(bytes)

 File "./python3.3/json/__init__.py", line 316, in loads

 File "./python3.3/json/decoder.py", line 351, in decode

 File "./python3.3/json/decoder.py", line 369, in raw_decode

ValueError: No JSON object could be decoded

我删除 // 运行 文件注释后。 F5 键起作用 fine.It 正是导致问题的注释。 Sublime Text 使用 JSON 作为配置文件,很多配置文件都带有 // 风格的注释。正如我们所知,评论已从 JSON 中删除。

那sublime text怎么允许在配置文件中进行注释,是不是用了管道?如果是,我的按键绑定怎么会失败?

Sublime 本身(核心程序,不是像 SublimeREPL 这样的插件)使用内部 JSON 库来解析配置文件,如 .sublime-settings.sublime-menu.sublime-build 等. 这个(很可能是定制的)解析器允许注释。

但是,插件是 运行 通过链接到 Sublime plugin_host 可执行文件的 Python 版本(目前开发版本为 3.3.6)。任何导入标准库的 json 模块(例如 run_existing_command.py 的插件都必须遵守该模块的限制,包括无法识别 JavaScript 风格的注释,例如 //在 JSON.

一个解决方法是将外部模块(如 commentjson that strips various types of comments, including //, before passing the data on to the standard json module. Since it is a pure Python module, you could just copy the source 目录导入到主 SublimeREPL 目录中,然后适当编辑 run_existing_command.py - 将第 6 行更改为 import commentjson as json 然后你'一切就绪。