2016-07-24 120 views
0

在Sublime Text中使用JSON配置文件中的註釋可能會使JSON對象無法解碼。這是我的故事。爲什麼Sublime Text 3允許在JSON配置文件中添加註釋?

我在我的Sublime Text 3中新安裝了SublimeREPL插件3.很快,我發現它默認運行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中移除。

然後,如何使Sublime文本允許在配置文件中進行註釋,是否使用管道?如果是這樣,我的密鑰綁定怎麼會失敗?

+1

** P租賃不要以文本形式發佈**。將文本複製並粘貼到您的問題中。圖像無法搜索,屏幕閱讀器無法解讀視覺障礙者的圖像。使用[編輯]鏈接修改您的問題。有關更多信息,請參見[this](http://meta.stackoverflow.com/a/285557/1426065)。 – MattDMo

+0

你說得對。我已經糾正了我的錯誤。感謝你指出了它。@ MattDMo – Naomi

回答

2

崇高本身(核心程序,如SublimeREPL不是插件)用來分析配置文件,如.sublime-settings.sublime-menu.sublime-build等內部JSON庫本(最有可能的定製)語法分析器允許發表評論。

但是,插件通過鏈接到Sublime plugin_host可執行文件的Python版本(目前爲開發版本3.3.6)運行。一個導入標準庫的json模塊的任何插件(如run_existing_command.py必須遵守該模塊的限制,這包括沒有認識到像JSON //的JavaScript風格的註釋。

一個解決辦法來,這將是進口的外部模塊如commentjson,在將數據傳遞到標準的json模塊之前,它會去除各種類型的註釋,包括//,由於它是純Python模塊,因此您可以將source目錄複製到主SublimeREPL目錄中,然後適當編輯run_existing_command.py - 將第6行更改爲import commentjson as json並且您已全部設置。

+0

在ST3中,你可以訪問內部庫並用'sublime.decode_value(string)'函數解碼一個json字符串。 –

+0

@ r-stein有趣,我從來沒有看過這個功能。不幸的是,SublimeREPL似乎在一段時間之前就被拋棄了,雖然它仍然運行良好,但任何新功能或錯誤修復的機會都在slim和none之間。 – MattDMo

+0

謝謝。這聽起來像一個有趣的解決方案。我會給它一個鏡頭。 @MattDMo – Naomi

相關問題