最简单的解决方案是使用json转储和加载
from json import loads, dumps
from collections import OrderedDict
def to_dict(input_ordered_dict):
return loads(dumps(input_ordered_dict))
注意:上面的代码适用于被json称为可序列化对象的字典。可以在此处找到默认对象类型的列表
因此,如果有序词典不包含特殊值,这应该就足够了。
编辑:基于注释,让我们改进上面的代码。让我们说,input_ordered_dict可能包含默认情况下无法通过json序列化的自定义类对象。在这种情况下,我们应该将default参数json.dumps与我们的自定义序列化程序一起使用。
(例如):
from collections import OrderedDict as odict
from json import loads, dumps
class Name(object):
def __init__(self, name):
name = name.split(" ", 1)
self.first_name = name[0]
self.last_name = name[-1]
a = odict()
a["thiru"] = Name("Mr Thiru")
a["wife"] = Name("Mrs Thiru")
a["type"] = "test" # This is by default serializable
def custom_serializer(obj):
if isinstance(obj, Name):
return obj.__dict__
b = dumps(a)
# Produces TypeError, as the Name objects are not serializable
b = dumps(a, default=custom_serializer)
# Produces desired output
此示例可以进一步扩展到更大的范围。我们甚至可以根据需要添加过滤器或修改值。只需在custom_serializer函数中添加其他部分
def custom_serializer(obj):
if isinstance(obj, Name):
return obj.__dict__
else:
# Will get into this if the value is not serializable by default
# and is not a Name class object
return None
from json import loads, dumps
from collections import OrderedDict
def custom_serializer(obj):
if isinstance(obj, Name):
return obj.__dict__
else:
# Will get into this if the value is not serializable by default
# and is also not a Name class object
return None
def to_dict(input_ordered_dict):
return loads(dumps(input_ordered_dict, default=custom_serializer))