Serialize python standard types (function, tuple, class, memoryview, set, frozenset, exceptions, complex, range, bytes, bytearray, dict with number keys, byte keys or tuple keys, and etc) with json. lambdaJSON lets you serialize python standard library objects with json.
I’ll show you some basic usage of the lambdaJSON lib. for more advanced examples please visit the examples section.
You can serialize any dicts with lambdaJSON supported keys (and also hashable). this includes dictionaries with byte keys, tuple keys and etc:
#!/usr/bin/env python
>>> import lambdaJSON
>>> myComplexData = {True: (3-5j), (3+5j): b'json', (1, 2, 3): {b'lambda': [1, 2, 3, (3, 4, 5)]}}
>>> mySerializedData = lambdaJSON.serialize(myComplexData)
>>> myComplexData == lambdaJSON.deserialize(mySerializedData)
True
>>>
To pass args and kwargs to the encoder/decoder simply pass them to the serialize/deserialize function, example for json:
>>> mySerializedData = lambdaJSON.serialize(myComplexData, sort_keys = True)
>>> myComplexData == lambdaJSON.deserialize(mySerializedData, object_hook = my_hook)
It can be done for ujson too.
You can also serialize python functions:
>>> import lambdaJSON
>>> def f(): print('lambdaJSON Rocks!')
>>> mySerializedFunction = lambdaJSON.serialize(f)
>>> myNewFunction = lambdaJSON.deserialize(mySerializedFunction)
>>> myNewFunction()
'lambdaJSON Rocks!'
>>>
You should pass a function that returns list of globals to while you are creating a lambdaJSON instance, if not (lambda: globals()) will be used:
>>> from lambdaJSON import lambdaJSON
>>> lj = lambdaJSON(globs = (lambda: globals()))
>>> y = 10
>>> def f(x): return x*y
>>> mySerializedFunction = lj.dumps(f)
>>> myNewFunction = lj.loads(mySerializedFunction)
>>> myNewFunction(5)
50
>>> y = 3
>>> myNewFunction(5)
15
>>>
If no globs passed to function, the globs will be just the __builtins__ module. Note that passing globals will pass the lambdaJSONs local globals and it will not work, if you want to include all the globals from where the deserialization function is called, just use globs = (lambda: globals()), else implement your own function. You can do some nice hacks too:
>>> 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
>>>
You can serialize Builtin Exceptions like this:
>>> a = lambdaJSON.serialize(OSError('FILE NOT FOUND'))
>>> b = lambdaJSON.deserialize(a)
>>> raise b
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
raise b
OSError: FILE NOT FOUND
>>>
introduced in version 0.2.15, you can now serialize basic classes and types. The support is basic, but I’m planning to develop the class serialization support in the next subversion. to deserialize a class, you must pass the globals function too, if you do not pass the globals, only __builtins__ will be passed to the class functions. this is an example to do it:
>>> class test(object):
def __init__(self):
self.var = 'lambdaJSON'
>>> serializedClass = lambdaJSON.serialize(test)
>>> newClass = lambdaJSON.deserialize(serializedClass)
>>> newClass().var
'lambdaJSON'
>>>
To check version, simply use lambdaJSON.__version__, or if you want to know which json lib is in use, try lambdaJSON.__json__
LambdaJSON first tries to import ujson, if it fails it will import simplejson, and if that fails too, the json lib will be imported. you can check which json lib is in use with lambdaJSON.__json__ variable.
This types are covered in this version: