| 1 |
import decimal |
| 2 |
from hashlib import md5 |
| 3 |
from warnings import warn |
| 4 |
|
| 5 |
import pytest |
| 6 |
from helper import SkippableTest |
| 7 |
|
| 8 |
import jsonpickle |
| 9 |
|
| 10 |
|
| 11 |
class Thing: |
| 12 |
def __init__(self, name): |
| 13 |
self.name = name |
| 14 |
self.child = None |
| 15 |
|
| 16 |
|
| 17 |
class A: |
| 18 |
def __init__(self): |
| 19 |
self.id = md5(str(id(self)).encode()).hexdigest()[:5] # unique enough hash |
| 20 |
|
| 21 |
|
| 22 |
class BSlots: |
| 23 |
__slots__ = ["a2", "a1", "a3"] |
| 24 |
|
| 25 |
def __init__(self): |
| 26 |
self.a2 = A() # set attribs not in alphabetical order |
| 27 |
self.a1 = A() |
| 28 |
self.a3 = self.a1 # create a reference |
| 29 |
|
| 30 |
|
| 31 |
SAMPLE_DATA = {'things': [Thing('data')]} |
| 32 |
|
| 33 |
|
| 34 |
class BackendBase(SkippableTest): |
| 35 |
def _is_installed(self, backend): |
| 36 |
if not jsonpickle.util.is_installed(backend): |
| 37 |
return self.skip('%s not available; please install' % backend) |
| 38 |
|
| 39 |
def set_backend(self, *args): |
| 40 |
backend = args[0] |
| 41 |
|
| 42 |
self._is_installed(backend) |
| 43 |
|
| 44 |
jsonpickle.load_backend(*args) |
| 45 |
jsonpickle.set_preferred_backend(backend) |
| 46 |
|
| 47 |
def set_preferred_backend(self, backend): |
| 48 |
self._is_installed(backend) |
| 49 |
jsonpickle.set_preferred_backend(backend) |
| 50 |
|
| 51 |
def tearDown(self): |
| 52 |
# always reset to default backend |
| 53 |
jsonpickle.set_preferred_backend('json') |
| 54 |
|
| 55 |
def assert_roundtrip(self, json_input): |
| 56 |
expect = SAMPLE_DATA |
| 57 |
actual = jsonpickle.decode(json_input) |
| 58 |
assert expect['things'][0].name == actual['things'][0].name |
| 59 |
assert expect['things'][0].child == actual['things'][0].child |
| 60 |
|
| 61 |
pickled = jsonpickle.encode(SAMPLE_DATA) |
| 62 |
actual = jsonpickle.decode(pickled) |
| 63 |
assert expect['things'][0].name == actual['things'][0].name |
| 64 |
assert expect['things'][0].child == actual['things'][0].child |
| 65 |
|
| 66 |
def test_None_dict_key(self): |
| 67 |
"""Ensure that backends produce the same result for None dict keys""" |
| 68 |
data = {None: None} |
| 69 |
expect = {'null': None} |
| 70 |
pickle = jsonpickle.encode(data) |
| 71 |
actual = jsonpickle.decode(pickle) |
| 72 |
assert expect == actual |
| 73 |
|
| 74 |
def test_encode_with_indent_and_separators(self): |
| 75 |
obj = { |
| 76 |
'a': 1, |
| 77 |
'b': 2, |
| 78 |
} |
| 79 |
expect = '{\n' ' "a": 1,\n' ' "b": 2\n' '}' |
| 80 |
actual = jsonpickle.encode(obj, indent=4, separators=(',', ': ')) |
| 81 |
assert expect == actual |
| 82 |
|
| 83 |
|
| 84 |
class JsonTestCase(BackendBase): |
| 85 |
def setUp(self): |
| 86 |
self.set_preferred_backend('json') |
| 87 |
|
| 88 |
def test_backend(self): |
| 89 |
expected_pickled = ( |
| 90 |
'{"things": [{' |
| 91 |
'"py/object": "backend_test.Thing", ' |
| 92 |
'"name": "data", ' |
| 93 |
'"child": null} ' |
| 94 |
']}' |
| 95 |
) |
| 96 |
self.assert_roundtrip(expected_pickled) |
| 97 |
|
| 98 |
|
| 99 |
class SimpleJsonTestCase(BackendBase): |
| 100 |
def setUp(self): |
| 101 |
self.set_preferred_backend('simplejson') |
| 102 |
|
| 103 |
def test_backend(self): |
| 104 |
expected_pickled = ( |
| 105 |
'{"things": [{' |
| 106 |
'"py/object": "backend_test.Thing", ' |
| 107 |
'"name": "data", ' |
| 108 |
'"child": null}' |
| 109 |
']}' |
| 110 |
) |
| 111 |
self.assert_roundtrip(expected_pickled) |
| 112 |
|
| 113 |
def test_decimal(self): |
| 114 |
# Default behavior: Decimal is preserved |
| 115 |
obj = decimal.Decimal(0.5) |
| 116 |
as_json = jsonpickle.dumps(obj) |
| 117 |
clone = jsonpickle.loads(as_json) |
| 118 |
assert isinstance(clone, decimal.Decimal) |
| 119 |
assert obj == clone |
| 120 |
|
| 121 |
# Custom behavior: we want to use simplejson's Decimal support. |
| 122 |
jsonpickle.set_encoder_options('simplejson', use_decimal=True, sort_keys=True) |
| 123 |
|
| 124 |
jsonpickle.set_decoder_options('simplejson', use_decimal=True) |
| 125 |
|
| 126 |
# use_decimal mode allows Decimal objects to pass-through to simplejson. |
| 127 |
# The end result is we get a simple '0.5' value as our json string. |
| 128 |
as_json = jsonpickle.dumps(obj, unpicklable=True, use_decimal=True) |
| 129 |
assert as_json == '0.5' |
| 130 |
# But when loading we get back a Decimal. |
| 131 |
clone = jsonpickle.loads(as_json) |
| 132 |
assert isinstance(clone, decimal.Decimal) |
| 133 |
|
| 134 |
# side-effect: floats become decimals too! |
| 135 |
obj = 0.5 |
| 136 |
as_json = jsonpickle.dumps(obj) |
| 137 |
clone = jsonpickle.loads(as_json) |
| 138 |
assert isinstance(clone, decimal.Decimal) |
| 139 |
# options are persisted unless we disable them |
| 140 |
jsonpickle.set_encoder_options('simplejson', use_decimal=False) |
| 141 |
jsonpickle.set_decoder_options('simplejson', use_decimal=False) |
| 142 |
|
| 143 |
def test_sort_keys(self): |
| 144 |
jsonpickle.set_encoder_options('simplejson', sort_keys=True) |
| 145 |
b = BSlots() |
| 146 |
with pytest.raises(TypeError): |
| 147 |
jsonpickle.encode(b, keys=True, warn=True) |
| 148 |
# return encoder options to default |
| 149 |
jsonpickle.set_encoder_options('simplejson', sort_keys=False) |
| 150 |
|
| 151 |
|
| 152 |
def has_module(module): |
| 153 |
try: |
| 154 |
__import__(module) |
| 155 |
except ImportError: |
| 156 |
warn(module + ' module not available for testing, ' 'consider installing') |
| 157 |
return False |
| 158 |
return True |
| 159 |
|
| 160 |
|
| 161 |
class UJsonTestCase(BackendBase): |
| 162 |
def setUp(self): |
| 163 |
self.set_preferred_backend('ujson') |
| 164 |
|
| 165 |
def test_backend(self): |
| 166 |
expected_pickled = ( |
| 167 |
'{"things":[{' |
| 168 |
r'"py\/object":"backend_test.Thing",' |
| 169 |
'"name":"data","child":null}' |
| 170 |
']}' |
| 171 |
) |
| 172 |
self.assert_roundtrip(expected_pickled) |
| 173 |
|
| 174 |
|
| 175 |
class YamlTestCase(BackendBase): |
| 176 |
def setUp(self): |
| 177 |
self.set_preferred_backend('yaml') |
| 178 |
|
| 179 |
def test_backend(self): |
| 180 |
expected_pickled = ( |
| 181 |
'things:\n' |
| 182 |
' - py/object: backend_test.Thing\n' |
| 183 |
' name: data\n' |
| 184 |
' child: null\n' |
| 185 |
) |
| 186 |
self.assert_roundtrip(expected_pickled) |