Debian Codegraph @

README.rst

raw
1 .. image:: https://img.shields.io/pypi/v/jsonpickle.svg
2 :target: `PyPI link`_
3
4 .. image:: https://img.shields.io/pypi/pyversions/jsonpickle.svg
5 :target: `PyPI link`_
6
7 .. _PyPI link: https://pypi.org/project/jsonpickle
8
9 .. image:: https://readthedocs.org/projects/jsonpickle/badge/?version=latest
10 :target: https://jsonpickle.readthedocs.io/en/latest/?badge=latest
11
12 .. image:: https://github.com/jsonpickle/jsonpickle/actions/workflows/test.yml/badge.svg
13 :target: https://github.com/jsonpickle/jsonpickle/actions
14 :alt: Github Actions
15
16 .. image:: https://img.shields.io/badge/License-BSD%203--Clause-blue.svg
17 :target: https://github.com/jsonpickle/jsonpickle/blob/main/COPYING
18 :alt: BSD
19
20
21 jsonpickle
22 ==========
23
24 jsonpickle is a library for the two-way conversion of complex Python objects
25 and `JSON <http://json.org/>`_. jsonpickle builds upon existing JSON
26 encoders, such as simplejson, json, and ujson.
27
28 .. warning::
29
30 jsonpickle can execute arbitrary Python code.
31
32 Please see the Security section for more details.
33
34
35 For complete documentation, please visit the
36 `jsonpickle documentation <http://jsonpickle.readthedocs.io/>`_.
37
38 Bug reports and merge requests are encouraged at the
39 `jsonpickle repository on github <https://github.com/jsonpickle/jsonpickle>`_.
40
41 Usage
42 =====
43 The following is a very simple example of how one can use jsonpickle in their scripts/projects. Note the usage of jsonpickle.encode and decode, and how the data is written/encoded to a file and then read/decoded from the file.
44
45 .. code-block:: python
46
47 import jsonpickle
48 from dataclasses import dataclass
49
50 @dataclass
51 class Example:
52 data: str
53
54
55 ex = Example("value1")
56 encoded_instance = jsonpickle.encode(ex)
57 assert encoded_instance == '{"py/object": "__main__.Example", "data": "value1"}'
58
59 with open("example.json", "w+") as f:
60 f.write(encoded_instance)
61
62 with open("example.json", "r+") as f:
63 written_instance = f.read()
64 decoded_instance = jsonpickle.decode(written_instance)
65 assert decoded_instance == ex
66
67 For more examples, see the `examples directory on GitHub <https://github.com/jsonpickle/jsonpickle/tree/main/examples>`_ for example scripts. These can be run on your local machine to see how jsonpickle works and behaves, and how to use it. Contributions from users regarding how they use jsonpickle are welcome!
68
69
70 Why jsonpickle?
71 ===============
72
73 Data serialized with python's pickle (or cPickle or dill) is not easily readable outside of python. Using the json format, jsonpickle allows simple data types to be stored in a human-readable format, and more complex data types such as numpy arrays and pandas dataframes, to be machine-readable on any platform that supports json. E.g., unlike pickled data, jsonpickled data stored in an Amazon S3 bucket is indexible by Amazon's Athena.
74
75 Security
76 ========
77
78 jsonpickle should be treated the same as the
79 `Python stdlib pickle module <https://docs.python.org/3/library/pickle.html>`_
80 from a security perspective.
81
82 .. warning::
83
84 The jsonpickle module **is not secure**. Only unpickle data you trust.
85
86 It is possible to construct malicious pickle data which will **execute
87 arbitrary code during unpickling**. Never unpickle data that could have come
88 from an untrusted source, or that could have been tampered with.
89
90 Consider signing data with an HMAC if you need to ensure that it has not
91 been tampered with.
92
93 Safer deserialization approaches, such as reading JSON directly,
94 may be more appropriate if you are processing untrusted data.
95
96
97 Install
98 =======
99
100 Install from pip for the latest stable release:
101
102 ::
103
104 pip install jsonpickle
105
106 Install from github for the latest changes:
107
108 ::
109
110 pip install git+https://github.com/jsonpickle/jsonpickle.git
111
112
113 Numpy/Pandas Support
114 ====================
115
116 jsonpickle includes built-in numpy and pandas extensions. If you would
117 like to encode sklearn models, numpy arrays, pandas DataFrames, and other
118 numpy/pandas-based data, then you must enable the numpy and/or pandas
119 extensions by registering their handlers::
120
121 >>> import jsonpickle.ext.numpy as jsonpickle_numpy
122 >>> import jsonpickle.ext.pandas as jsonpickle_pandas
123 >>> jsonpickle_numpy.register_handlers()
124 >>> jsonpickle_pandas.register_handlers()
125
126
127 Development
128 ===========
129
130 Use `make` to run the unit tests::
131
132 make test
133
134 `pytest` is used to run unit tests internally.
135
136 A `tox` target is provided to run tests using all installed and supported Python versions::
137
138 make tox
139
140 `jsonpickle` itself has no dependencies beyond the Python stdlib.
141 `tox` is required for testing when using the `tox` test runner only.
142
143 The testing requirements are specified in `setup.cfg`.
144 It is recommended to create a virtualenv and run tests from within the
145 virtualenv.::
146
147 python3 -mvenv env3
148 source env3/bin/activate
149 pip install --editable '.[dev]'
150 make test
151
152 You can also use a tool such as `vx <https://github.com/davvid/vx/>`_
153 to activate the virtualenv without polluting your shell environment::
154
155 python3 -mvenv env3
156 vx env3 pip install --editable '.[dev]'
157 vx env3 make test
158
159 If you can't use a venv, you can install the testing packages as follows::
160
161 pip install .[testing]
162
163 `jsonpickle` supports multiple Python versions, so using a combination of
164 multiple virtualenvs and `tox` is useful in order to catch compatibility
165 issues when developing.
166
167 GPG Signing
168 ===========
169
170 Unfortunately, while versions of jsonpickle before 3.0.1 should still be signed, GPG signing support was removed from PyPi (https://blog.pypi.org/posts/2023-05-23-removing-pgp/) back in May 2023.
171
172 License
173 =======
174
175 Licensed under the BSD License. See COPYING for details.