我有一个Point Class 绘图类,其成员是Point的列表
我想将Drawing类放入sqlalchemy的sqlite记录中,为此我需要序列化Point列表。
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def encode_Point(obj):
if isinstance(obj, Point):
return obj.__dict__
return obj
class Drawing:
def __init__(self):
self.m_points = []
self.m_name = "Foo"
在编码时:
pointsJsonDump = dumps(drawing.m_points, default=encode_Point)
i.execute(name='Bar', points=pointsJsonDump)
'转储'函数给出的是例如:
[{"y": 71, "x": 373}, {"y": 104, "x": 342}, {"y": 183, "x": 338}, {"y": 200, "x": 433}, {"y": 140, "x": 504}]
然后我将此字符串存储到带有sqlalchemy的sqlite表中。
现在我希望能够再次将此记录检索到Python对象中: 所以我盯着看:
s = self.m_drawingsTable.select()
rs = s.execute()
for row in rs:
d = Drawing()
# Put the record into a Python object
# Here i put each field into a member of an object
# Ex: d.name = row.name
但是如何从json对象重建Point []?