jsonalchemy: add de_coerce to reverse serialization of list and dict elements from Mutation objects back to original state.

This commit is contained in:
Marcin Kuzminski 2017-11-26 17:18:28 +01:00
parent b6f2cdd95b
commit d113a429a9

View file

@ -99,6 +99,9 @@ class MutationObj(Mutable):
return MutationList.coerce(key, value)
return value
def de_coerce(self):
return self
@classmethod
def _listen_on_attribute(cls, attribute, coerce, parent_cls):
key = attribute.key
@ -158,6 +161,9 @@ class MutationDict(MutationObj, DictClass):
self._key = key
return self
def de_coerce(self):
return dict(self)
def __setitem__(self, key, value):
# Due to the way OrderedDict works, this is called during __init__.
# At this time we don't have a key set, but what is more, the value
@ -177,7 +183,7 @@ class MutationDict(MutationObj, DictClass):
def __reduce_ex__(self, proto):
# support pickling of MutationDicts
d = dict(self)
return (self.__class__, (d, ))
return (self.__class__, (d,))
class MutationList(MutationObj, list):
@ -188,6 +194,9 @@ class MutationList(MutationObj, list):
self._key = key
return self
def de_coerce(self):
return list(self)
def __setitem__(self, idx, value):
list.__setitem__(self, idx, MutationObj.coerce(self._key, value))
self.changed()