Merge pull request #113 from dzhibas/fix-st3/issue-110

fix: implement duplicate key detection
This commit is contained in:
Terminal 2020-04-17 18:51:09 -04:00 committed by GitHub
commit 394353d5bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 1 deletions

View File

@ -192,10 +192,28 @@ class PrettyJsonValidate(PrettyJsonBaseCommand, sublime_plugin.TextCommand):
try:
self.json_loads(self.view.substr(selection))
sublime.message_dialog("JSON is Valid")
except Exception:
self.show_exception()
sublime.message_dialog("Invalid JSON")
return
try:
decoder = json.JSONDecoder(object_pairs_hook=self.duplicate_key_hook)
decoder.decode(self.view.substr(selection))
except Exception as ex:
self.show_exception()
sublime.message_dialog("Invalid JSON")
return
sublime.message_dialog("Invalid JSON")
def duplicate_key_hook(self, pairs):
result = dict()
for key, val in pairs:
if key in result:
raise KeyError("Duplicate key specified: %s" % key)
result[key] = val
return result
class PrettyJsonCommand(PrettyJsonBaseCommand, sublime_plugin.TextCommand):