在Sublime Text中的JSON配置文件中使用注释会使JSON对象无法解码。这是我的故事。
我在Sublime Text 3中新安装了SublimeREPL插件。不久,我发现它运行的是Python2.7而不是默认的3.5,因此我根据SublimeREPL Docs添加了自己的Python3.5配置文件以使其支持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"}
}
}
]}
]
}]
}]
注意这里有一个注释//运行文件。通过菜单栏工具-> 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键工作正常,正是引起问题的注释。Sublime Text使用JSON作为配置文件,许多配置文件都带有//样式注释。我们知道,注释是通过设计从JSON中删除的。
那么升华文本如何允许在配置文件中添加注释,是否使用管道?如果是,我的键绑定怎么会失败?
升华本身(核心程序,没有插件,像SublimeREPL)使用解析配置文件就像一个内部JSON库.sublime-settings
,.sublime-menu
,.sublime-build
等这个(最有可能的定制)语法分析器允许发表评论。
但是,插件是通过链接到Sublimeplugin_host
可执行文件的Python版本(对于开发版本,当前为3.3.6)运行。导入标准库json
模块的任何插件(例如run_existing_command.py
必须遵守该模块的限制,并且包括无法识别//
JSON中的JavaScript样式的注释)。
一种解决方法是在将数据传递到标准模块之前,导入一个外部模块commentjson
,以剥离各种类型的注释,包括。由于它是一个纯Python模块,因此您只需将源目录复制到主SublimeREPL目录中,然后进行适当的编辑-将第6行更改为,就一切就绪。//
json
run_existing_command.py
import commentjson as json
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句