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

21 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# Export other names not in __all__ 

69from .backend import JSONBackend # noqa: F401 

70from .backend import json 

71from .handlers import register # noqa: F401 

72from .handlers import unregister # noqa: F401 

73from .pickler import Pickler # noqa: F401 

74from .pickler import encode 

75from .unpickler import Unpickler # noqa: F401 

76from .unpickler import decode 

77from .version import __version__ # noqa: F401 

78 

79__all__ = ('encode', 'decode') 

80 

81# register built-in handlers 

82__import__('jsonpickle.handlers', level=0) 

83 

84# Export specific JSONPluginMgr methods into the jsonpickle namespace 

85set_preferred_backend = json.set_preferred_backend 

86set_decoder_options = json.set_decoder_options 

87set_encoder_options = json.set_encoder_options 

88load_backend = json.load_backend 

89remove_backend = json.remove_backend 

90enable_fallthrough = json.enable_fallthrough 

91 

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

93dumps = encode 

94loads = decode