为什么Sublime Text 3允许在JSON配置文件中发表评论?

时间:2016-07-24 13:09:46

标签: json sublimetext3 sublime-text-plugin sublimerepl

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

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

请注意,此文件中有一条注释//运行文件。这个配置可以从菜单栏工具 - > SublimeREPL-> Python3.5中正常工作。但是,当我尝试使用repl_python3.5_run绑定F5键以便更轻松地访问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键工作正常。这正是导致问题的注释。 Sublime Text使用JSON作为配置文件,许多配置文件带有//样式注释。我们知道,评论会从设计中删除。

那么sublime文本如何允许配置文件中的注释,是否使用管道?如果是,我的密钥绑定怎么会失败?

1 个答案:

答案 0 :(得分:3)

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

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

对此的一种解决方法是导入一个外部模块,如commentjson,在将数据传递到标准//模块之前删除各种类型的注释,包括json。由于它是纯Python模块,您可以将source目录复制到主SublimeREPL目录中,然后适当地编辑run_existing_command.py - 将第6行更改为import commentjson as json并且您已完成设置。