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

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}

Download & Install

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

Contact

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

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

Authors

Change Log

Version 0.4.0 - June 21, 2011
  • Switch build from setuptools to distutils
  • Consistent dictionary key ordering
  • Fix areas with improper support for unpicklable=False
  • Added support for cyclical data structures (#16).
  • Experimental support for jsonlib and py-yajl backends.
  • New contributers David K. Hess and Alec Thomas

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.

Version 0.3.1 - December 12, 2009
  • Include tests and docs directories in sdist for distribution packages.
Version 0.3.0 - December 11, 2009
  • List and set subclasses.
  • Objects with module references.
  • Newstyle classes with __slots__.
  • Objects implementing __setstate__() and __getstate__() (follows the pickle protocol).
  • Improved support for Zope objects via pre-fetch.
  • Support for user-defined serialization handlers via the jsonpickle.handlers registry.
  • Removed cjson support per John Millikin’s recommendation.
  • General improvements to style, including PEP 257 compliance and refactored project layout.
  • Steps towards Python 2.3 and Python 3 support.
  • New contributors Dan Buch and Ian Schenck.
  • Thanks also to Kieran Darcy, Eoghan Murray, and Antonin Hildebrand for their assistance!
Version 0.2.0 - January 10, 2009
  • Support for all major Python JSON backends (including json in Python 2.6, simplejson, cjson, and demjson)
  • Handle several datetime objects using the repr() of the objects (Thanks to Antonin Hildebrand).
  • Sphinx documentation
  • Added support for recursive data structures
  • Unicode dict-keys support
  • Support for Google App Engine and Django
  • Tons of additional testing and bug reports (Antonin Hildebrand, Sorin, Roberto Saccon, Faber Fedor, FirePython, and Joose)
Version 0.1.0 - August 21, 2008
  • Added long as basic primitive (thanks Adam Fisk)
  • Prefer python-cjson to simplejson, if available
  • Major API change, use python-cjson’s decode/encode instead of simplejson’s load/loads/dump/dumps
  • Added benchmark.py to compare simplejson and python-cjson
Version 0.0.5 - July 21, 2008
  • Changed prefix of special fields to conform with CouchDB requirements (Thanks Dean Landolt). Break backwards compatibility.
  • Moved to Google Code subversion
  • Fixed unit test imports
Version 0.0.3
  • Convert back to setup.py from pavement.py (issue found by spidaman)
Version 0.0.2
  • Handle feedparser’s FeedParserDict
  • Converted project to Paver
  • Restructured directories
  • Increase test coverage
Version 0.0.1
Initial release

License

jsonpickle is provided under a New BSD license,

Copyright (C) 2008-2011 John Paulett (john -at- paulett.org)

Table Of Contents

Next topic

jsonpickle API

This Page