Skip to main content

Serialize python standard types (function, tuple, memoryview, set, frozenset, complex, range, bytes, bytearray, dict with number keys, byte keys or tuple keys, and etc) with json. No dependencies, just needs json lib (or other json libraries). --Changes: Added support for memoryview

Project description

Serialize python standard types (function, tuple, complex, memoryview, set, frozenset, range, bytearray, bytes, dict with number keys, byte keys or tuple keys, and etc) with json. lambdaJSON lets you serialize python standard library objects with json. Typical usage:

#!/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!'
>>>

Changed int 0.2.4, for function deserialization you must pass a function which returns the list of globals for the function:

>>> import lambdaJSON
>>> y = 10
>>> def f(x): return x*y

>>> mySerializedFunction = lambdaJSON.serialize(f)
>>> myNewFunction  = lambdaJSON.deserialize(mySerializedFunction, globs = (lambda: globals()))
>>> 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 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

>>> mySerializedFunction = lambdaJSON.serialize(f)
>>> myNewFunction  = lambdaJSON.deserialize(mySerializedFunction, globs = g)
>>> myNewFunction(2,3)
17
>>> myNewFunction(2,3)
18
>>>

isn’t it cool??

To check version, simply use lambdaJSON.__version__, or if you want to know which json lib is in use, try lambdaJSON.__json__

After the support for all types are added, I’m planning to release a query friendly version of this library, that will be in version 0.3.0.

Currently Supported Types

This types are covered in this version:

  1. Functions

  2. Bytes and Bytearrays

  3. Tuples

  4. Complex

  5. Range

  6. Set and Frozenset

  7. Memoryview

  8. Dicts (With Number, Tuple, String, Bool and Byte keys)

  9. other json supported types

Changes from previous

Added support for memoryview

Project Info

Github project page: https://github.com/pooya-eghbali/lambdaJSON Mail me at: persian.writer [at] Gmail.com

Contact me if there were any issues or you had a feature request. you can also post issues on github.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lambdaJSON-0.2.12.zip (9.2 kB view details)

Uploaded Source

File details

Details for the file lambdaJSON-0.2.12.zip.

File metadata

  • Download URL: lambdaJSON-0.2.12.zip
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for lambdaJSON-0.2.12.zip
Algorithm Hash digest
SHA256 6a0f9a22ff32598c15ba104acf723e581c919f0118c1af1f37f1af9be177d455
MD5 20887037313ca1b87d7a288a4e49d016
BLAKE2b-256 92147a3b46ae08fe2a342f6ea01ab3d0df5a167b824b97e6df032e198afc5f96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page