Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_encoding_methods(self):
converter = JsonConverter()
def encoder(obj):
return {"a": [val * 2 for val in obj.a]}
def decoder(obj):
obj["a"] = [val // 2 for val in obj["a"]]
return A(**obj)
converter.register(A, encoder=encoder, decoder=decoder)
a = A([1, 2, 3])
res = converter.dumps(a)
res_dict = json.loads(res)
self.assertEqual(res_dict, {"__id__": "pelita.test.A", "__value__": {"a": [2, 4, 6]}})
reencoded = converter.loads(res)
def test_autoregistration_explicit(self):
json_converter = JsonConverter()
@json_converter.serializable("pelita.test.Autoserialize")
class Autoserialize(object):
def __init__(self, attr):
self.attr = attr
def _to_json_dict(self):
return {"attr": self.attr}
@classmethod
def _from_json_dict(cls, item):
return cls(**item)
def __eq__(self, other):
return self.attr == other.attr
def test_autoregistration_implicit(self):
json_converter = JsonConverter()
@json_converter.serializable
class Autoserialize(object):
def __init__(self, attr):
self.attr = attr
def _to_json_dict(self):
return {"attr": self.attr}
@classmethod
def _from_json_dict(cls, item):
return cls(**item)
def __eq__(self, other):
return self.attr == other.attr
def test_can_decode(self):
converter = JsonConverter()
converter.register(A)
json_code = """{"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}"""
decoded = converter.loads(json_code)
a = A(['1', '2', '3'])
self.assertEqual(decoded, a)
def test_double_identifier(self):
class AA(A):
pass
converter = JsonConverter()
converter.register(A)
self.assertRaises(ValueError, converter.register, AA)
def test_unregistered(self):
converter = JsonConverter()
a = A("value")
self.assertRaises(TypeError, converter.dumps, a)
def test_wrong_classes(self):
converter = JsonConverter()
class NoId(object):
def _to_json_dict(self):
return {}
@classmethod
def _from_json_dict(cls, item):
return cls()
self.assertRaises(ValueError, converter.register, NoId)
@json_id("pelita.test.NoToDict")
class NoToDict(object):
@classmethod
def _from_json_dict(cls, item):
return cls()
def test_can_recode_nested(self):
converter = JsonConverter()
converter.register(A, encoder=A._to_json_dict, decoder=A._from_json_dict)
converter.register(B)
a1 = A(1)
a2 = A("2")
a3 = A(['1', '2', '3'])
bb = B("B", a1, a2, a3)
dumped = converter.dumps(bb)
loaded = converter.loads(dumped)
self.assertEqual(bb, loaded)
def test_can_encode(self):
converter = JsonConverter()
converter.register(A)
a = A(['1', '2', '3'])
encoded = converter.dumps(a)
should_be = {"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}
decoded = json.loads(encoded)
self.assertEqual(decoded, should_be)
return wrapper
def dumps(self, obj):
return json.dumps(obj, default=self.encode)
def loads(self, json_dict):
return json.loads(json_dict, object_hook=self.decode)
def json_id(id):
def wrapper(cls):
cls._json_id = id
return cls
return wrapper
# default serialization helpers
json_converter = JsonConverter()
serializable = json_converter.serializable