Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/jsonpickle/__init__.py: 95%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Copyright (C) 2008 John Paulett (john -at- paulett.org)
2# Copyright (C) 2009, 2011, 2013 David Aguilar (davvid -at- gmail.com)
3# All rights reserved.
4#
5# This software is licensed as described in the file COPYING, which
6# you should have received as part of this distribution.
8"""Python library for serializing any arbitrary object graph into JSON.
10.. warning::
12 The jsonpickle module **is not secure**. Only unpickle data you trust.
14 It is possible to construct malicious pickle data which will **execute
15 arbitrary code during unpickling**. Never unpickle data that could have come
16 from an untrusted source, or that could have been tampered with.
18 Consider signing data with an HMAC if you need to ensure that it has not
19 been tampered with.
21 Safer deserialization approaches, such as reading the raw JSON
22 directly, may be more appropriate if you are processing untrusted data.
24jsonpickle can take almost any Python object and turn the object into JSON.
25Additionally, it can reconstitute the object back into Python.
27The object must be accessible globally via a module and must
28inherit from object (AKA new-style classes).
30Create an object::
32 class Thing(object):
33 def __init__(self, name):
34 self.name = name
36 obj = Thing('Awesome')
38Use jsonpickle to transform the object into a JSON string::
40 import jsonpickle
41 frozen = jsonpickle.encode(obj)
43Use jsonpickle to recreate a Python object from a JSON string::
45 thawed = jsonpickle.decode(frozen)
47The new object has the same type and data, but essentially is now a copy of
48the original.
50.. code-block:: python
52 assert obj.name == thawed.name
54If you will never need to load (regenerate the Python class from JSON), you can
55pass in the keyword unpicklable=False to prevent extra information from being
56added to JSON::
58 oneway = jsonpickle.encode(obj, unpicklable=False)
59 result = jsonpickle.decode(oneway)
60 assert obj.name == result['name'] == 'Awesome'
62.. note::
64 Please see the note in the :ref:`api-docs` when serializing dictionaries
65 that contain non-string dictionary keys.
67"""
69# Export other names not in __all__
70from .backend import JSONBackend # noqa: F401
71from .backend import json
72from .handlers import register # noqa: F401
73from .handlers import unregister # noqa: F401
74from .pickler import Pickler # noqa: F401
75from .pickler import encode
76from .unpickler import Unpickler # noqa: F401
77from .unpickler import decode
78from .version import __version__ # noqa: F401
80__all__ = ("encode", "decode")
82# register built-in handlers
83__import__("jsonpickle.handlers", level=0)
85# Export specific JSONPluginMgr methods into the jsonpickle namespace
86set_preferred_backend = json.set_preferred_backend
87set_decoder_options = json.set_decoder_options
88set_encoder_options = json.set_encoder_options
89load_backend = json.load_backend
90remove_backend = json.remove_backend
91enable_fallthrough = json.enable_fallthrough
93# json.load(), loads(), dump(), dumps() compatibility
94dumps = encode
95loads = decode