Debian Codegraph @

docs/api.rst

raw
1 .. _jsonpickle-api:
2
3 ==============
4 jsonpickle API
5 ==============
6
7 .. testsetup:: *
8
9 import jsonpickle
10 import jsonpickle.pickler
11 import jsonpickle.unpickler
12 import jsonpickle.handlers
13 import jsonpickle.util
14
15 .. contents::
16
17 .. _api-docs:
18
19 :mod:`jsonpickle` -- High Level API
20 ===================================
21
22 .. note::
23
24 For performance and compatibility reasons, jsonpickle does not preserve
25 non-string dictionary keys by default. This results in a simpler, but
26 sometimes lossy, JSON representation.
27
28 Specify ``keys=True`` when encoding and decoding to preserve integers,
29 tuples, and other non-strings types as dictionary keys.
30
31 .. autofunction:: jsonpickle.encode
32
33 .. autofunction:: jsonpickle.decode
34
35 Choosing and Loading Backends
36 -----------------------------
37
38 jsonpickle allows the user to specify what JSON backend to use
39 when encoding and decoding. By default, jsonpickle will try to use, in
40 the following order: ``simplejson`` and :mod:`json`.
41 The preferred backend can be set via :func:`jsonpickle.set_preferred_backend`.
42 Additional JSON backends can be used via :func:`jsonpickle.load_backend`.
43
44 For example, users of `Django <http://www.djangoproject.com/>`_ can use the
45 version of simplejson that is bundled in Django::
46
47 jsonpickle.load_backend('django.util.simplejson', 'dumps', 'loads', ValueError))
48 jsonpickle.set_preferred_backend('django.util.simplejson')
49
50 Supported backends:
51
52 * :mod:`json`
53 * `simplejson <http://undefined.org/python/#simplejson>`_
54
55 Experimental backends:
56
57 * `jsonlib <https://pypi.org/project/jsonlib/>`_
58 * yajl via `py-yajl <https://github.com/rtyler/py-yajl/>`_
59 * `ujson <https://pypi.org/project/ujson/>`_
60
61 .. autofunction:: jsonpickle.set_preferred_backend
62
63 .. autofunction:: jsonpickle.load_backend
64
65 .. autofunction:: jsonpickle.remove_backend
66
67 .. autofunction:: jsonpickle.set_encoder_options
68
69 .. autofunction:: jsonpickle.set_decoder_options
70
71 Customizing JSON output
72 -----------------------
73
74 jsonpickle supports the standard :mod:`pickle` `__getstate__` and `__setstate__`
75 protocol for representing object instances.
76
77 .. method:: object.__getstate__()
78
79 Classes can further influence how their instances are pickled; if the class
80 defines the method :meth:`__getstate__`, it is called and the return state is
81 pickled as the contents for the instance, instead of the contents of the
82 instance's dictionary. If there is no :meth:`__getstate__` method, the
83 instance's :attr:`__dict__` is pickled.
84
85 .. method:: object.__setstate__(state)
86
87 Upon unpickling, if the class also defines the method :meth:`__setstate__`,
88 it is called with the unpickled state. If there is no
89 :meth:`__setstate__` method, the pickled state must be a dictionary and its
90 items are assigned to the new instance's dictionary. If a class defines both
91 :meth:`__getstate__` and :meth:`__setstate__`, the state object needn't be a
92 dictionary and these methods can do what they want.
93
94 .. py:attribute:: object._jsonpickle_exclude = set()
95
96 Classes can specify attributes that they want to exclude from being pickled
97 by defining an attribute named `_jsonpickle_exclude`. This set should contain
98 the names of attributes of that class to exclude from being pickled.
99
100
101 :mod:`jsonpickle.handlers` -- Custom Serialization Handlers
102 -----------------------------------------------------------
103
104 The `jsonpickle.handlers` module allows plugging in custom
105 serialization handlers at run-time. This feature is useful when
106 jsonpickle is unable to serialize objects that are not
107 under your direct control.
108
109 .. automodule:: jsonpickle.handlers
110 :members:
111 :undoc-members:
112
113 Low Level API
114 =============
115
116 Typically this low level functionality is not needed by clients.
117
118 Note that arguments like ``safe=True`` do not make it safe to load an untrusted
119 jsonpickle string.
120
121 :mod:`jsonpickle.pickler` -- Python to JSON-compatible dict
122 -----------------------------------------------------------
123
124 .. automodule:: jsonpickle.pickler
125 :members:
126 :undoc-members:
127
128 :mod:`jsonpickle.unpickler` -- JSON-compatible dict to Python
129 -------------------------------------------------------------
130
131 .. automodule:: jsonpickle.unpickler
132 :members:
133 :undoc-members:
134
135 :mod:`jsonpickle.backend` -- JSON Backend Management
136 ----------------------------------------------------
137
138 .. automodule:: jsonpickle.backend
139 :members:
140
141 :mod:`jsonpickle.util` -- Helper functions
142 ------------------------------------------
143
144 .. automodule:: jsonpickle.util
145 :members:
146 :undoc-members: