import jsonpickle import jsonpickle.pickler import jsonpickle.unpickler import jsonpickle.handlers import jsonpickle.util
jsonpickle.encode
jsonpickle.decode
jsonpickle allows the user to specify what JSON backend to use when encoding and decoding. By default, jsonpickle will try to use, in the following order: simplejson
, json
, and demjson
. The prefered backend can be set via jsonpickle.set_preferred_backend
. Additional JSON backends can be used via jsonpickle.load_backend
.
For example, users of Django can use the version of simplejson that is bundled in Django:
jsonpickle.load_backend('django.util.simplejson', 'dumps', 'loads', ValueError))
jsonpickle.set_preferred_backend('django.util.simplejson')
Supported backends:
json
- simplejson
- demjson
Experimental backends:
jsonpickle.set_preferred_backend
jsonpickle.load_backend
jsonpickle.remove_backend
jsonpickle.set_encoder_options
jsonpickle.set_decoder_options
jsonpickle supports the standard pickle
__getstate__ and __setstate__ protocol for representating object instances.
object.__getstate__()
Classes can further influence how their instances are pickled; if the class defines the method __getstate__
, it is called and the return state is pickled as the contents for the instance, instead of the contents of the instance's dictionary. If there is no __getstate__
method, the instance's __dict__
is pickled.
object.__setstate__(state)
Upon unpickling, if the class also defines the method __setstate__
, it is called with the unpickled state. If there is no __setstate__
method, the pickled state must be a dictionary and its items are assigned to the new instance's dictionary. If a class defines both __getstate__
and __setstate__
, the state object needn't be a dictionary and these methods can do what they want.
The jsonpickle.handlers module allows plugging in custom serialization handlers at run-time. This feature is useful when jsonpickle is unable to serialize objects that are not under your direct control.
jsonpickle.handlers
Typically this low level functionality is not needed by clients.
Note that arguments like safe=True
do not make it safe to load an untrusted jsonpickle string.
jsonpickle.pickler
jsonpickle.unpickler
jsonpickle.backend
jsonpickle.util