Re:Show HN: Kim – A Python serialization and marshaling fram...
by amelius on Wednesday April 12, 2017 @08:50AM
(#14097098)
Can it serialize cycles?
[ Parent
| on HN ]
Re:Show HN: Kim – A Python serialization and marshaling fram...
by mikeywaites on Wednesday April 12, 2017 @09:04AM
(#14097221)
Hey Amelius,
thanks for the message. Gonna be honest, I'm not sure what you mean by cycles. Can you elaborate a bit?
Re:Show HN: Kim – A Python serialization and marshaling fram...
by amelius on Wednesday April 12, 2017 @09:13AM
(#14097283)
Roughly speaking, by cycles I mean a structure that refers to itself somehow. For example:
A = {}
B = {}
A["ref"] = B
B["ref"] = A
So would it be possible to serialize A and B, and of course to deserialize them?Note that
print A
gives {'ref': {'ref': {...}}}
which is of course not a suitable serialization, since you can't recover the original structure from it.
Re:Show HN: Kim – A Python serialization and marshaling fram...
by jackqu7 on Wednesday April 12, 2017 @09:24AM
(#14097386)
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}
Re:Show HN: Kim – A Python serialization and marshaling fram...
by arnarbi on Wednesday April 12, 2017 @02:25PM
(#14100372)
That's not a solution as it will not restore the same object graph, it will just repeat values.
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
Re:Show HN: Kim – A Python serialization and marshaling fram...
by amelius on Wednesday April 12, 2017 @11:02AM
(#14098311)
The output isn't clear to me:
{'nest': {'back': {'score': 5}, 'name': 'test'}, 'score': 5}
How can this be mapped back unambiguously to a cyclic structure?(I might be wrong but it seems to me that the act of serialization has simply expanded the cycle one level deep.)