Python: 无法使用 jsonpickle 打印同一对象两次或更多次
Python: unable to print the same object twice or more using jsonpickle
在一个相当复杂的项目中,我在某个时候想到了这个问题,应该打印嵌套对象并且相同的对象必须存在两次或更多次。这里我提供一个简化版本的代码来重现问题:
import jsonpickle
class nickname:
def __init__(self, name:str, id:int):
self.name = name
self.id = id
class test_class:
def __init__(self, name:str, age:int, nicknames:[nickname]):
self.name = name
self.age = age
self.nicknames = nicknames
nicknames = []
nicknames.append(nickname('Bomber', 1))
nicknames.append(nickname('Roccia', 2))
test_dict = {}
test_dict['key1'] = test_class('Gigi', 12, nicknames)
test_dict['key2'] = test_class('Sandro', 14, nicknames)
test_list = []
test_list.append(test_dict['key1'])
test_list.append(test_dict['key2'])
test_list.append(test_dict['key1'])
print(jsonpickle.encode(test_list, unpicklable=False))
这给出了输出:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": [null, null]}, null]
在哪里可以看到重复对象为空。添加 make_refs=False 参数会导致:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": "[<__main__.nickname object at 0x00BF50D0>, <__main__.nickname object at 0x00BF50F0>]"}, "<__main__.test_class object at 0x00BF5110>"]
存在对象引用,但仍未对其进行编码。
有人知道如何解决这个问题吗?当然,我希望重印重复的对象而不是 "null" 个字段。
谢谢
我相信 json pickle 不支持打印出重复的对象。相反,它提供了对它的引用。另一种方法是使用 json.dumps 并使用 lambda 表达式递归转换为字典。
import json
class nickname:
def __init__(self, name:str, id:int):
self.name = name
self.id = id
class person:
def __init__(self, name:str, age:int, nicknames:[nickname]):
self.name = name
self.age = age
self.nicknames = nicknames
nicknames = [nickname('Bomber', 1), nickname('Roccia', 2)]
test_list = [person('Gigi', 12, nicknames), person('Sandro', 14, nicknames)]
results = json.dumps(test_list, default=lambda x: x.__dict__)
print(results)
这输出:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}]
在一个相当复杂的项目中,我在某个时候想到了这个问题,应该打印嵌套对象并且相同的对象必须存在两次或更多次。这里我提供一个简化版本的代码来重现问题:
import jsonpickle
class nickname:
def __init__(self, name:str, id:int):
self.name = name
self.id = id
class test_class:
def __init__(self, name:str, age:int, nicknames:[nickname]):
self.name = name
self.age = age
self.nicknames = nicknames
nicknames = []
nicknames.append(nickname('Bomber', 1))
nicknames.append(nickname('Roccia', 2))
test_dict = {}
test_dict['key1'] = test_class('Gigi', 12, nicknames)
test_dict['key2'] = test_class('Sandro', 14, nicknames)
test_list = []
test_list.append(test_dict['key1'])
test_list.append(test_dict['key2'])
test_list.append(test_dict['key1'])
print(jsonpickle.encode(test_list, unpicklable=False))
这给出了输出:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": [null, null]}, null]
在哪里可以看到重复对象为空。添加 make_refs=False 参数会导致:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": "[<__main__.nickname object at 0x00BF50D0>, <__main__.nickname object at 0x00BF50F0>]"}, "<__main__.test_class object at 0x00BF5110>"]
存在对象引用,但仍未对其进行编码。
有人知道如何解决这个问题吗?当然,我希望重印重复的对象而不是 "null" 个字段。
谢谢
我相信 json pickle 不支持打印出重复的对象。相反,它提供了对它的引用。另一种方法是使用 json.dumps 并使用 lambda 表达式递归转换为字典。
import json
class nickname:
def __init__(self, name:str, id:int):
self.name = name
self.id = id
class person:
def __init__(self, name:str, age:int, nicknames:[nickname]):
self.name = name
self.age = age
self.nicknames = nicknames
nicknames = [nickname('Bomber', 1), nickname('Roccia', 2)]
test_list = [person('Gigi', 12, nicknames), person('Sandro', 14, nicknames)]
results = json.dumps(test_list, default=lambda x: x.__dict__)
print(results)
这输出:
[{"name": "Gigi", "age": 12, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}, {"name": "Sandro", "age": 14, "nicknames": [{"name": "Bomber", "id": 1}, {"name": "Roccia", "id": 2}]}]