JSON loading now generally ensures loaded value type matches the template
This commit is contained in:
1 parent
df05fb2707
commit
ea4437b9d2
1 file changed
+6
-8
@@ -223,16 +223,14 @@ def merge_json(obj: JsonType, template: Mapping[Any, Any]) -> None:
|
|||||||
# NOTE: This modifies object in place
|
# NOTE: This modifies object in place
|
||||||
for k, v in list(obj.items()):
|
for k, v in list(obj.items()):
|
||||||
if k not in template:
|
if k not in template:
|
||||||
|
# unknown key: overwrite from template
|
||||||
del obj[k]
|
del obj[k]
|
||||||
elif isinstance(v, dict):
|
elif type(v) is not type(template[k]):
|
||||||
if isinstance(template[k], dict):
|
# types don't match: overwrite from template
|
||||||
merge_json(v, template[k])
|
|
||||||
else:
|
|
||||||
# object is a dict, template is not: overwrite from template
|
|
||||||
obj[k] = template[k]
|
|
||||||
elif isinstance(template[k], dict):
|
|
||||||
# template is a dict, object is not: overwrite from template
|
|
||||||
obj[k] = template[k]
|
obj[k] = template[k]
|
||||||
|
elif isinstance(v, dict):
|
||||||
|
assert isinstance(template[k], dict)
|
||||||
|
merge_json(v, template[k])
|
||||||
# ensure the object is not missing any keys
|
# ensure the object is not missing any keys
|
||||||
for k in template.keys():
|
for k in template.keys():
|
||||||
if k not in obj:
|
if k not in obj:
|
||||||
|
|||||||
Reference in new issue
Block a user