Is there any support of this in jsonpickle?
E.g. I store and object, them modify its schema, then try to load it back.
The following change, for instance, (attribute addition)
import jsonpickle
class Stam(object):
def __init__(self, a):
self.a = a
def __str__(self):
return '%s with a=%s' % (self.__class__.__name__, str(self.a))
js = jsonpickle.encode(Stam(123))
print 'encoded:', js
class Stam(object):
def __init__(self, a, b):
self.a = a
self.b = b
def __str__(self):
return '%s with a=%s, b=%s' % (self.__class__.__name__, str(self.a), str(self.b))
s=jsonpickle.decode(js)
print 'decoded:', s
produces an error:
encoded: {"py/object": "__main__.Stam", "a": 123}
decoded: Traceback (most recent call last):
File "C:\gae\google\appengine\ext\admin\__init__.py", line 317, in post
exec(compiled_code, globals())
File "<string>", line 25, in <module>
File "<string>", line 22, in __str__
AttributeError: 'Stam' object has no attribute 'b'
There is no support for type evolution or type migrations within jsonpickle.
Your best course of action would be to load (via json.loads
) the JSON representation of your data into a basic Python structure of lists / dicts / strings / numbers. Traverse this Python representation, adding in empty/default b
keys. Then re-save the JSON via json.dumps
.
You can then use jsonpickle to load the modified version of the data.
temp = json.loads(js)
temp['b'] = None
js = json.dumps(temp)
jsonpickle.decode(js)
This obviously gets more complicated if your object model is more complex, but you can check the py/object key to see if you need to modify the object.