49 lines
1.1 KiB
Python
49 lines
1.1 KiB
Python
import json
|
|
import os
|
|
|
|
# Make it easy to read a json file
|
|
def read_json(path, default_value={}):
|
|
if os.path.isfile(path):
|
|
try:
|
|
with open(path, "r") as file:
|
|
json_string = file.read()
|
|
json_dict = json.loads(json_string)
|
|
return json_dict
|
|
except:
|
|
pass
|
|
return default_value
|
|
|
|
# Merge dictionary b into a
|
|
def merge_dicts(a, b):
|
|
if b is None:
|
|
return a
|
|
|
|
output = a
|
|
|
|
for k in b.keys():
|
|
if k not in output.keys():
|
|
if b[k] is not None:
|
|
output[k] = b[k]
|
|
|
|
if b[k] is None:
|
|
if k in output.keys():
|
|
output.pop(k, None)
|
|
|
|
if isinstance(b[k], dict):
|
|
if isinstance(output[k], dict):
|
|
output[k] = merge_dicts(output[k], b[k])
|
|
continue
|
|
|
|
output[k] = b[k]
|
|
if output[k] is None:
|
|
del output[k]
|
|
|
|
return output
|
|
|
|
def write_json(path, o):
|
|
try:
|
|
with open(path, "w") as file:
|
|
file.write(json.dumps(o, indent=4))
|
|
except:
|
|
pass
|