jsonpickle is a Python library for serialization and deserialization of complex Python objects to and from JSON. The standard Python libraries for encoding Python into JSON, such as the stdlib’s json, simplejson, and demjson, can only handle Python primitives that have a direct JSON equivalent (e.g. dicts, lists, strings, ints, etc.). jsonpickle builds on top of these libraries and allows more complex data structures to be serialized to JSON. jsonpickle is highly configurable and extendable–allowing the user to choose the JSON backend and add additional backends.
Contents
Python library for serializing any arbitrary object graph into JSON. It can take almost any Python object and turn the object into JSON. Additionally, it can reconstitute the object back into Python.
>>> import jsonpickle
>>> from samples import Thing
Create an object.
>>> obj = Thing('A String')
>>> print obj.name
A String
Use jsonpickle to transform the object into a JSON string.
>>> pickled = jsonpickle.encode(obj)
>>> print pickled
{"py/object": "samples.Thing", "name": "A String", "child": null}
Use jsonpickle to recreate a Python object from a JSON string
>>> unpickled = jsonpickle.decode(pickled)
>>> str(unpickled.name)
'A String'
Warning
Loading a JSON string from an untrusted source represents a potential security vulnerability. jsonpickle makes no attempt to sanitize the input.
The new object has the same type and data, but essentially is now a copy of the original.
>>> obj == unpickled
False
>>> obj.name == unpickled.name
True
>>> type(obj) == type(unpickled)
True
If you will never need to load (regenerate the Python class from JSON), you can pass in the keyword unpicklable=False to prevent extra information from being added to JSON.
>>> oneway = jsonpickle.encode(obj, unpicklable=False)
>>> print oneway
{"name": "A String", "child": null}
The easiest way to get jsonpickle is via PyPi with pip:
$ pip install -U jsonpickle
For Python 2.6+, jsonpickle has no required dependencies (it uses the standard library’s json module by default). For Python 2.5 or earlier, you must install a supported JSON backend (including simplejson or demjson). For example:
$ pip install simplejson
You can also download or checkout the latest code and install from source:
$ python setup.py install
Please join our mailing list. You can send email to jsonpickle@googlegroups.com.
Check http://github.com/jsonpickle/jsonpickle for project updates.
- John Paulett - john -at- paulett.org - http://github.com/johnpaulett
- David Aguilar - davvid -at- gmail.com - http://github.com/davvid
- Dan Buch - http://github.com/meatballhat
- Ian Schenck - http://github.com/ianschenck
- David K. Hess - http://github.com/davidkhess
- Alec Thomas - http://github.com/alecthomas
Warning
To support cyclical data structures (#16), the storage format has been modified. Efforts have been made to ensure backwards-compatibility. jsonpickle 0.4.0 can read data encoded by jsonpickle 0.3.1, but earlier versions of jsonpickle may be unable to read data encoded by jsonpickle 0.4.0.
- List and set subclasses.
- Objects with module references.
- Newstyle classes with __slots__.
- Objects implementing __setstate__() and __getstate__() (follows the pickle protocol).
jsonpickle is provided under a New BSD license,
Copyright (C) 2008-2011 John Paulett (john -at- paulett.org)