diff options
author | Nick Brassel <nick@tzarc.org> | 2022-11-28 07:54:00 +1100 |
---|---|---|
committer | Nick Brassel <nick@tzarc.org> | 2022-11-28 07:54:00 +1100 |
commit | 4020674163fc80914059c4c9c3be5c0ae00bd150 (patch) | |
tree | 6f4187d72b04d03572adf507502afbda9726d696 /lib/python/qmk/json_schema.py | |
parent | 8f9b49dc5b05fd3421e47aa76822a5b2199dfca6 (diff) | |
parent | 9e78e65a566487b2f4fe7b663971a01deb6ddad2 (diff) |
Merge remote-tracking branch 'upstream/develop'
Diffstat (limited to 'lib/python/qmk/json_schema.py')
-rw-r--r-- | lib/python/qmk/json_schema.py | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/python/qmk/json_schema.py b/lib/python/qmk/json_schema.py index 01175146b5..934e2f841f 100644 --- a/lib/python/qmk/json_schema.py +++ b/lib/python/qmk/json_schema.py @@ -10,7 +10,18 @@ import jsonschema from milc import cli -def json_load(json_file): +def _dict_raise_on_duplicates(ordered_pairs): + """Reject duplicate keys.""" + d = {} + for k, v in ordered_pairs: + if k in d: + raise ValueError("duplicate key: %r" % (k,)) + else: + d[k] = v + return d + + +def json_load(json_file, strict=True): """Load a json file from disk. Note: file must be a Path object. @@ -20,7 +31,7 @@ def json_load(json_file): # Not necessary if the data is provided via stdin if isinstance(json_file, Path): json_file = json_file.open(encoding='utf-8') - return hjson.load(json_file) + return hjson.load(json_file, object_pairs_hook=_dict_raise_on_duplicates if strict else None) except (json.decoder.JSONDecodeError, hjson.HjsonDecodeError) as e: cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e) |