Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# This seems fine.
loaded1 = jsons.load({'value': 42}, cls=TestOptionalInt)
self.assertEqual(42, loaded1.value)
# Strings are parsed if possible.
loaded2 = jsons.load({'value': '42'}, cls=TestOptionalInt)
self.assertEqual(42, loaded2.value)
# No value or None will result in None.
loaded3 = jsons.load({}, cls=TestOptionalInt)
loaded4 = jsons.load({'value': None}, cls=TestOptionalInt)
self.assertEqual(None, loaded3.value)
self.assertEqual(None, loaded4.value)
# Now this will fail.
with self.assertRaises(DeserializationError):
jsons.load({'value': 'not good'}, cls=TestOptionalInt)
def test_load_partially_deserialized_dict_in_strict_mode(self):
class C:
def __init__(self, d: datetime.datetime):
self.d = d
dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
tzinfo=datetime.timezone.utc)
dumped = {'d': dat}
with self.assertRaises(DeserializationError):
jsons.load(dumped, C, strict=True)
self.assertEqual(None, jsons.load({'x': None}, A).x)
# Test Optional with a value.
self.assertEqual(1, jsons.load({'x': 1}, B).x)
# Test Optional with None value.
self.assertEqual(None, jsons.load({'x': None}, B).x)
# Test Optional without value.
self.assertEqual(None, jsons.load({}, B).x)
# Test Union with a value.
self.assertEqual(1, jsons.load({'x': {'x': 1}}, C).x.x)
# Test Union with invalid value.
with self.assertRaises(DeserializationError):
jsons.load({'x': 'no match in the union'}, C).x
def test_load_complex_number(self):
dumped = {'real': 1.0, 'imag': 2.0}
loaded = jsons.load(dumped, complex)
self.assertEqual(loaded, 1+2j)
bad_keys_dump = {'some_key': 3.0}
with self.assertRaises(DeserializationError):
jsons.load(bad_keys_dump, complex)
bad_types_dump = {'real': 'some_string', 'imag': {'a': 2}}
with self.assertRaises(DeserializationError):
jsons.load(bad_types_dump, complex)
class A:
def __init__(self, x):
self.x = x
class B:
def __init__(self, x: Optional[int]):
self.x = x
class C:
def __init__(self, x: Union[datetime.datetime, A]):
self.x = x
self.assertEqual(1, jsons.load({'x': 1}, B).x)
self.assertEqual(None, jsons.load({'x': None}, B).x)
self.assertEqual(1, jsons.load({'x': {'x': 1}}, C).x.x)
with self.assertRaises(DeserializationError):
jsons.load({'x': 'no match in the union'}, C).x
def test_exception_wrong_bytes(self):
with self.assertRaises(DeserializationError):
jsons.loadb('{"key": "value"}')
def test_load_and_cast(self):
class C:
def __init__(self, x: int):
self.x = x
self.assertEqual(42, jsons.load('42', int))
self.assertEqual(42.0, jsons.load('42', float))
self.assertEqual('42', jsons.load(42, str))
self.assertEqual(True, jsons.load(42, bool))
with self.assertRaises(DeserializationError):
jsons.load('fortytwo', int)
try:
jsons.load('fortytwo', int)
except DeserializationError as err:
self.assertEqual('fortytwo', err.source)
self.assertEqual(int, err.target)
def test_load_partially_deserialized_dict_in_strict_mode(self):
class C:
def __init__(self, d: datetime.datetime):
self.d = d
dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
tzinfo=datetime.timezone.utc)
dumped = {'d': dat}
with self.assertRaises(DeserializationError):
jsons.load(dumped, C, strict=True)
def test_undefined_deserializer(self):
dumped = {'id': 12, 'birthday': '1879-03-14T11:30:00+01:00'}
with self.assertRaises(DeserializationError) as errorContext:
jsons.load(dumped, WrongUser)
error = errorContext.exception
self.assertEqual('No deserializer for type "datetime"', error.message)
self.assertEqual(datetime, error.target)
class C:
def __init__(self, x: int):
self.x = x
self.assertEqual(42, jsons.load('42', int))
self.assertEqual(42.0, jsons.load('42', float))
self.assertEqual('42', jsons.load(42, str))
self.assertEqual(True, jsons.load(42, bool))
with self.assertRaises(DeserializationError):
jsons.load('fortytwo', int)
try:
jsons.load('fortytwo', int)
except DeserializationError as err:
self.assertEqual('fortytwo', err.source)
self.assertEqual(int, err.target)