|
|
|
|
|
by jackqu7
3355 days ago
|
|
Yes, this is possible as long as the second level nested object has a role to stop infinite recursion from occurring. Cycles are not automatically detected. class BaseMapper(Mapper):
__type__ = TestType
score = Integer()
nest = Nested('NestedMapper')
__roles__ = {'nested': blacklist('nest')}
class NestedMapper(Mapper):
__type__ = TestType
back = Nested('BaseMapper', role='nested')
name = String()
obj2 = TestType(name='test')
obj = TestType(score=5, nest=obj2)
obj2.back = obj
>> BaseMapper(obj=obj).serialize()
{'nest': {'back': {'score': 5}, 'name': 'test'}, 'score': 5}
|
|
One way is to to store a table of objects (as identified by id()) encountered during serialization, indexed by the order you encounter them. If you encounter an object you have already serialized, serialize an index into that table. On deserialization, construct the same kind of table, and deserialize an index with a reference to the same object.
See e.g. AMF for an example format that does this: https://en.wikipedia.org/wiki/Action_Message_Format