Here you will find some more advanced examples of using the lambdaJSON lib.
It is easy to serialize a class if it is just inherited from builtin types, but serializing classes that are inherited from third party classes is a little tricky. see the examples to find out how to serialize these classes with lambdaJSON.
When you are deserializing a class, lambdaJSON first searches for bases of the class inside the globals, if it did not find the base class it will try builtins, if not found an exception will be raised.
if you serialize a class that is inherited by a QtGui.QWidget, you will find out that the base class for this class is <class ‘PySide.QtGui.QWidget’>, so in order to deserialize this class, you need to have ‘PySide.QtGui.QWidget’ in your globals dict (or just the globals you pass to deserialization function).
Simply importing QtGui from PySide will not work, and i will not create a function to search for the base class inside the globals because of security issues (idk if i change my mind or not). here is how you can serialize these classes:
>>> from lambdaJSON import lambdaJSON
>>> from PySide import QtGui
>>> class myWidget(QtGui.QWidget):
pass
>>> lj = lambdaJSON(globs = lambda: {'PySide.QtGui.QWidget': QtGui.QWidget})
>>> serializedClass = lj.dumps(myWidget)
>>> newClass = lj.loads(serializedClass)
I could develop lambdaJSON in a way that you just could pass a dictionary with references as globals, but i decided to accept functions instead of a dict, to add more controling power. with this, you can do some nice hacks, and one of them is this. The example source is:
>>> z = 10
>>> def g():
global z
z += 1
return {'z':z}
>>> def f(x,y): return x*y+z
>>> from lambdaJSON import lambdaJSON
>>> lj = lambdaJSON(globs = g)
>>> mySerializedFunction = lj.dumps(f)
>>> myNewFunction = lj.loads(mySerializedFunction)
>>> myNewFunction(2,3)
17
>>> myNewFunction(2,3)
18
>>>