JSON 序列化期间仅对一部分数据进行自定义格式设置
Custom formatting for just a portion of data during JSON serialization
我有一个要序列化的 Python 对象树。它的结构是这样的:
{
"regular_object": {
...
},
"regular_field": ...
"special_object": {
"f1": "v1",
"f2": v2,
...
"fn": "vn"
}
}
我想序列化这样的对象树,以便它在 JSON 表示中像这样格式化:
{
"regular_object": {
...
},
"regular_field": ...
"special_object": { "f1": "v1", "f2": v2, ..., "fn": "vn" }
}
换句话说,我想要基于被格式化对象的内容的混合格式。
我尝试通过覆盖其 default
方法并使用自定义 class 对象来存储 special_object
的内容来玩弄 json.JSONEncoder
,以便自定义 default
被调用,像这样:
class SpecialObject:
def __init__(self, attrs: dict):
self.attrs = attrs
class SpecialEncoder(json.JSONEncoder):
def default(self, o):
JE = json.JSONEncoder
if isinstance(o, SpecialObject):
return f"{{ ', '.join([f'{JE.encode(self, el[0])}: {JE.encode(el[1])}' for el in o.attrs.items()]) }}"
else:
return super().default(o)
但看起来 JSONEncoder
再次对 default
的 return 值进行了编码 ,因此我为自定义类型生成的字符串被序列化为字符串,输出类似于:
{
...
"special_object": "{ \"f1\": \"v1\", ..., \"fn\": \"vn\" }"
}
这显然不是我想要的。我也尝试过像这里的一些答案所建议的那样覆盖 JSONEncoder.encode
方法,但是该方法永远不会从 JSONEncoder
本身内部调用。
我确实发现 several approaches 可以解决看似相似的问题,但是那里提供的解决方案对于像我这样的任务来说看起来太可怕而且过于复杂。
有没有一种方法可以在一两行代码中实现我所需要的,而无需深入 JSONEncoder
的肠子?它不需要是一个通用的解决方案,只是我可以快速解决的问题,并且可能只适用于一个案例。
实际上,你可以这样做,但它是...你看看它的样子。
def custom_json_dumps(obj, *args, keys_to_skip_indent=(), **kwargs):
if isinstance(obj, dict):
indent = kwargs.pop("indent", 0)
separators = kwargs.get("separators", (", ", ": "))
res = "{" + "\n" * int(indent != 0)
for k, v in obj.items():
if k in keys_to_skip_indent or indent == 0:
encoded = json.dumps(v, *args, **kwargs)
else:
encoded = json.dumps(v, *args, indent=indent, **kwargs)
res += "\"{}\"".format(k) + separators[1] + encoded + separators[0] + "\n" * int(indent != 0)
return res[:res.rindex(separators[0])].replace("\n", "\n" + " " * indent) + "\n" * int(indent != 0) + "}"
else:
return json.dumps(obj, *args, **kwargs)
测试:
o = {
"regular_object": {
"a": "b"
},
"regular_field": 100000,
"float_test": 1.0000001,
"bool_test": True,
"list_test": ["1", 0, 1.32, {"a": "b"}],
"special_object": {
"f1": "v1",
"f2": "v2",
"fn": "vn"
}
}
print(custom_json_dumps(o, keys_to_skip_indent={"special_object", "list_test"}, indent=4))
输出:
{
"regular_object": {
"a": "b"
},
"regular_field": 100000,
"float_test": 1.0000001,
"bool_test": true,
"list_test": ["1", 0, 1.32, {"a": "b"}],
"special_object": {"f1": "v1", "f2": "v2", "fn": "vn"}
}
我有一个要序列化的 Python 对象树。它的结构是这样的:
{
"regular_object": {
...
},
"regular_field": ...
"special_object": {
"f1": "v1",
"f2": v2,
...
"fn": "vn"
}
}
我想序列化这样的对象树,以便它在 JSON 表示中像这样格式化:
{
"regular_object": {
...
},
"regular_field": ...
"special_object": { "f1": "v1", "f2": v2, ..., "fn": "vn" }
}
换句话说,我想要基于被格式化对象的内容的混合格式。
我尝试通过覆盖其 default
方法并使用自定义 class 对象来存储 special_object
的内容来玩弄 json.JSONEncoder
,以便自定义 default
被调用,像这样:
class SpecialObject:
def __init__(self, attrs: dict):
self.attrs = attrs
class SpecialEncoder(json.JSONEncoder):
def default(self, o):
JE = json.JSONEncoder
if isinstance(o, SpecialObject):
return f"{{ ', '.join([f'{JE.encode(self, el[0])}: {JE.encode(el[1])}' for el in o.attrs.items()]) }}"
else:
return super().default(o)
但看起来 JSONEncoder
再次对 default
的 return 值进行了编码 ,因此我为自定义类型生成的字符串被序列化为字符串,输出类似于:
{
...
"special_object": "{ \"f1\": \"v1\", ..., \"fn\": \"vn\" }"
}
这显然不是我想要的。我也尝试过像这里的一些答案所建议的那样覆盖 JSONEncoder.encode
方法,但是该方法永远不会从 JSONEncoder
本身内部调用。
我确实发现 several approaches 可以解决看似相似的问题,但是那里提供的解决方案对于像我这样的任务来说看起来太可怕而且过于复杂。
有没有一种方法可以在一两行代码中实现我所需要的,而无需深入 JSONEncoder
的肠子?它不需要是一个通用的解决方案,只是我可以快速解决的问题,并且可能只适用于一个案例。
实际上,你可以这样做,但它是...你看看它的样子。
def custom_json_dumps(obj, *args, keys_to_skip_indent=(), **kwargs):
if isinstance(obj, dict):
indent = kwargs.pop("indent", 0)
separators = kwargs.get("separators", (", ", ": "))
res = "{" + "\n" * int(indent != 0)
for k, v in obj.items():
if k in keys_to_skip_indent or indent == 0:
encoded = json.dumps(v, *args, **kwargs)
else:
encoded = json.dumps(v, *args, indent=indent, **kwargs)
res += "\"{}\"".format(k) + separators[1] + encoded + separators[0] + "\n" * int(indent != 0)
return res[:res.rindex(separators[0])].replace("\n", "\n" + " " * indent) + "\n" * int(indent != 0) + "}"
else:
return json.dumps(obj, *args, **kwargs)
测试:
o = {
"regular_object": {
"a": "b"
},
"regular_field": 100000,
"float_test": 1.0000001,
"bool_test": True,
"list_test": ["1", 0, 1.32, {"a": "b"}],
"special_object": {
"f1": "v1",
"f2": "v2",
"fn": "vn"
}
}
print(custom_json_dumps(o, keys_to_skip_indent={"special_object", "list_test"}, indent=4))
输出:
{
"regular_object": {
"a": "b"
},
"regular_field": 100000,
"float_test": 1.0000001,
"bool_test": true,
"list_test": ["1", 0, 1.32, {"a": "b"}],
"special_object": {"f1": "v1", "f2": "v2", "fn": "vn"}
}