Reject json with duplicate keys? (#18108)

This commit is contained in:
Joel Challis 2022-11-09 15:50:43 +00:00 committed by GitHub
parent 293dbfdeff
commit dc9162438d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -10,7 +10,18 @@ import jsonschema
from milc import cli 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. """Load a json file from disk.
Note: file must be a Path object. 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 # Not necessary if the data is provided via stdin
if isinstance(json_file, Path): if isinstance(json_file, Path):
json_file = json_file.open(encoding='utf-8') 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: 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) cli.log.error('Invalid JSON encountered attempting to load {fg_cyan}%s{fg_reset}:\n\t{fg_red}%s', json_file, e)