Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/jsonpickle/__init__.py: 94%

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

17 statements  

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. 

7 

8"""Python library for serializing any arbitrary object graph into JSON. 

9 

10.. warning:: 

11 

12 The jsonpickle module **is not secure**. Only unpickle data you trust. 

13 

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. 

17 

18 Consider signing data with an HMAC if you need to ensure that it has not 

19 been tampered with. 

20 

21 Safer deserialization approaches, such as reading the raw JSON 

22 directly, may be more appropriate if you are processing untrusted data. 

23 

24jsonpickle can take almost any Python object and turn the object into JSON. 

25Additionally, it can reconstitute the object back into Python. 

26 

27The object must be accessible globally via a module and must 

28inherit from object (AKA new-style classes). 

29 

30Create an object:: 

31 

32 class Thing(object): 

33 def __init__(self, name): 

34 self.name = name 

35 

36 obj = Thing('Awesome') 

37 

38Use jsonpickle to transform the object into a JSON string:: 

39 

40 import jsonpickle 

41 frozen = jsonpickle.encode(obj) 

42 

43Use jsonpickle to recreate a Python object from a JSON string:: 

44 

45 thawed = jsonpickle.decode(frozen) 

46 

47The new object has the same type and data, but essentially is now a copy of 

48the original. 

49 

50.. code-block:: python 

51 

52 assert obj.name == thawed.name 

53 

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:: 

57 

58 oneway = jsonpickle.encode(obj, unpicklable=False) 

59 result = jsonpickle.decode(oneway) 

60 assert obj.name == result['name'] == 'Awesome' 

61 

62.. note:: 

63 

64 Please see the note in the :ref:`api-docs` when serializing dictionaries 

65 that contain non-string dictionary keys. 

66 

67""" 

68 

69# Export other names not in __all__ 

70from .backend import JSONBackend, json # noqa: F401 

71from .handlers import register, unregister # noqa: F401 

72from .pickler import Pickler, encode # noqa: F401 

73from .unpickler import Unpickler, decode # noqa: F401 

74from .version import __version__ # noqa: F401 

75 

76__all__ = ("encode", "decode") # ruff: ignore[RUF022] 

77 

78# register built-in handlers 

79__import__("jsonpickle.handlers", level=0) 

80 

81# Export specific JSONPluginMgr methods into the jsonpickle namespace 

82set_preferred_backend = json.set_preferred_backend 

83set_decoder_options = json.set_decoder_options 

84set_encoder_options = json.set_encoder_options 

85load_backend = json.load_backend 

86remove_backend = json.remove_backend 

87enable_fallthrough = json.enable_fallthrough 

88 

89# json.load(), loads(), dump(), dumps() compatibility 

90dumps = encode 

91loads = decode