[英]JSON serialization of dictionary with complex objects
I am trying to serialize the dictionary playersElo
for saving/loading it as/from JSON.我正在尝试序列化字典
playersElo
以将其作为/从 JSON 保存/加载。 But as it's not a serializable object and I can't find a way to do it.但由于它不是可序列化的 object ,所以我找不到办法。
playersElo={} # dictionnary of {<int> : <PlayerElo>}
playersElo[1] = PlayerElo()
playersElo[2] = PlayerElo()
...
class PlayerElo:
"""
A class to represent a player in the Elo Rating System
"""
def __init__(self, name: str, id: str, rating):
self.id = id
self.name = name
# comment the 2 lines below in order to start with a rating associated
# to current player rank
self.eloratings = {0: 1500}
self.elomatches = {0: 0}
self.initialrating = rating
Maybe this can be a starting spot for you.也许这可以成为您的起点。 The serializer grabs the
__dict__
attribute from the object and makes a new dict-of-dicts, then writes it to JSON.序列化程序从 object 中获取
__dict__
属性并创建一个新的 dict-of-dicts,然后将其写入 JSON。 The deserializer creates a dummy object, then updates the __dict__
on the way in.解串器创建一个虚拟 object,然后在进入的过程中更新
__dict__
。
import json
class PlayerElo:
"""
A class to represent a player in the Elo Rating System
"""
def __init__(self, name: str, id: str, rating):
self.id = id
self.name = name
self.eloratings = {0: 1500}
self.elomatches = {0: 0}
self.initialrating = rating
playersElo={} # dictionnary of {<int> : <PlayerElo>}
playersElo[1] = PlayerElo('Joe','123',999)
playersElo[2] = PlayerElo('Bill','456',1999)
def serialize(ratings):
newdict = {i:j.__dict__ for i,j in ratings.items()}
json.dump( newdict, open('x.json','w') )
def deserialize():
o = json.load(open('x.json'))
pe = {}
for k,v in o.items():
obj = PlayerElo('0','0',0)
obj.__dict__.update( o )
pe[int(k)] = obj
return pe
print(playersElo)
serialize( playersElo )
pe = deserialize( )
print(pe)
You can extend the json.JSONEncoder
to handle instances of your class.您可以扩展
json.JSONEncoder
来处理 class 的实例。 There are examples in the module's documentation , but here one why of doing it with your PlayerElo
class.模块文档中有示例,但这里有一个使用
PlayerElo
class 的原因。
Note: Also see my answer to the question Making object JSON serializable with regular encoder for a more generic way of do it.注意:另请参阅我对使用常规编码器可序列化 object JSON问题的回答,以获得更通用的方法。
import json
class PlayerElo:
""" A class to represent a player in the Elo Rating System. """
def __init__(self, name: str, id: str, rating):
self.id = id
self.name = name
# comment the 2 lines below in order to start with a rating associated
# to current player rank
self.eloratings = {0: 1500}
self.elomatches = {0: 0}
self.initialrating = rating
class MyJSONEcoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, PlayerElo):
return dict(type='PlayerElo', name=obj.name, id = obj.id,
rating=obj.initialrating)
return super().default(obj)
playersElo={} # dictionnary of {<int> : <PlayerElo>}
playersElo[1] = PlayerElo('Bob', 'thx1138', 4)
playersElo[2] = PlayerElo('Sue', 'p-138', 3)
from pprint import pprint
my_encoder = MyJSONEcoder()
pprint(my_encoder.encode(playersElo))
Here the JSON string it generated and printed:这里是它生成并打印的 JSON 字符串:
('{"1": {"type": "PlayerElo", "name": "Bob", "id": "thx1138", "rating": 4}, '
'"2": {"type": "PlayerElo", "name": "Sue", "id": "p-138", "rating": 3}}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.