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