jsonpickle Documentation

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.

jsonpickle Usage

Note

Please see the note in the jsonpickle – High Level API when serializing dictionaries that contain non-string dictionary keys.

Python library for serializing any arbitrary object graph into JSON.

Warning

jsonpickle can execute arbitrary Python code. Do not load jsonpickles from untrusted / unauthenticated sources.

jsonpickle can take almost any Python object and turn the object into JSON. Additionally, it can reconstitute the object back into Python.

The object must be accessible globally via a module and must inherit from object (AKA new-style classes).

Create an object:

class Thing(object):
    def __init__(self, name):
        self.name = name

obj = Thing('Awesome')

Use jsonpickle to transform the object into a JSON string:

import jsonpickle
frozen = jsonpickle.encode(obj)

Use jsonpickle to recreate a Python object from a JSON string:

thawed = jsonpickle.decode(frozen)

The new object has the same type and data, but essentially is now a copy of the original.

assert obj.name == thawed.name

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)
result = jsonpickle.decode(oneway)
assert obj.name == result['name'] == 'Awesome'

Download & Install

The easiest way to get jsonpickle is via PyPi with pip:

$ pip install -U jsonpickle

jsonpickle has no required dependencies (it uses the standard library’s json module by default).

You can also download or checkout the latest code and install from source:

$ python setup.py install

Contact

Please join our mailing list. You can send email to jsonpickle@googlegroups.com.

Check https://github.com/jsonpickle/jsonpickle for project updates.

Authors

License

jsonpickle is provided under a New BSD license,

Copyright (C) 2008-2011 John Paulett (john -at- paulett.org) Copyright (C) 2009-2016 David Aguilar (davvid -at- gmail.com)