dumps = dump_to_str
loads = load_from_str
另:
dump = dump_to_file
load = load_from_file
to_json_str、to_json_obj、to_json_file
import json
def to_json_str(obj):
return json.dumps(obj)
def to_json_file(obj, 文件对象, ensure_ascii=False, indent=2):
return json.dump(obj, 文件对象, ensure_ascii=ensure_ascii, indent=indent)
def to_json_obj(字符串or文件对象):
def from_str_to_dict():
return json.loads(字符串or文件对象)
def from_file_to_dict():
return json.load(字符串or文件对象)
def default():
raise Exception("参数类型未支持")
switch = {
"<class 'str'>": from_str_to_dict,
"<class '_io.TextIOWrapper'>": from_file_to_dict,
}
return switch.get(repr(type(字符串or文件对象)), default)()