How to use the jsons.load function in jsons

To help you get started, we’ve selected a few jsons examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ramonhagenaars / jsons / tests / test_object.py View on Github external
def test_load_object_with_default_value(self):
        class A:
            def __init__(self, x, y = 2):
                self.x = x
                self.y = y

        a = A(1)
        loaded_a = jsons.load({'x': 1}, A)
        self.assertEqual(a.x, loaded_a.x)
        self.assertEqual(a.y, loaded_a.y)
github ramonhagenaars / jsons / tests / test_validation.py View on Github external
def test_validation_valid(self):
        jsons.set_validator(lambda c: c.x > 5, C)
        jsons.load({'x': 6}, C)  # Should be fine.
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_load_float(self):
        self.assertEqual(123.456, jsons.load(123.456))
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_load_namedtuple(self):
        dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
                                tzinfo=datetime.timezone.utc)
        T = NamedTuple('T', [('x', str), ('y', datetime.datetime)])
        t = T('test', dat)
        loaded = jsons.load(['test', '2018-07-08T21:34:00Z'], T)
        self.assertEqual(t, loaded)

        T._field_defaults = dict(y=dat)
        loaded2 = jsons.load(['test'], T)
        self.assertEqual(t, loaded)

        with self.assertRaises(UnfulfilledArgumentError):
            jsons.load([], T)
        try:
            jsons.load([], T)
        except UnfulfilledArgumentError as err:
            self.assertEqual([], err.source)
            self.assertEqual(T, err.target)
            self.assertEqual('x', err.argument)
github ramonhagenaars / jsons / tests / test_complex_number.py View on Github external
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)
github ramonhagenaars / jsons / tests / test_specific_versions.py View on Github external
def test_uuid_serialization(self):
        from version_with_dataclasses import User
        user = User(uuid.uuid4(), 'name')

        dumped = jsons.dump(user)
        self.assertEqual(dumped['user_uuid'], str(user.user_uuid))

        loaded = jsons.load(dumped, User)
        self.assertEqual(user.user_uuid, loaded.user_uuid)

        self.assertEqual('name', loaded.name)
github ramonhagenaars / jsons / tests / test_list.py View on Github external
def test_load_list_multithreaded(self):
        dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
                                tzinfo=datetime.timezone.utc)
        list_ = [1, 2, 3, [4, 5, [dat]]]
        expectation = [1, 2, 3, [4, 5, ['2018-07-08T21:34:00Z']]]
        self.assertEqual(list_, jsons.load(expectation, tasks=3,
                                           task_type=Thread))

        with self.assertRaises(JsonsError):
            jsons.load(expectation, tasks=-1,
                       task_type=Thread)

        self.assertEqual([1], jsons.load(['1'], List[int], tasks=2,
                                         task_type=Thread))

        # More tasks than elements should still work.
        self.assertEqual([1, 1, 1, 1], jsons.load(['1', '1', '1', '1'],
                                                  List[int], tasks=16,
                                                  task_type=Thread))
github ramonhagenaars / jsons / tests / test_exception.py View on Github external
def test_exception_unfulfilled_arg(self):
        class C:
            def __init__(self, x: int, y: int):
                self.x = x
                self.y = y

        with self.assertRaises(UnfulfilledArgumentError):
            jsons.load({"x": 1}, C)

        try:
            jsons.load({"x": 1}, C)
        except UnfulfilledArgumentError as err:
            self.assertDictEqual({"x": 1}, err.source)
            self.assertEqual(C, err.target)
            self.assertEqual('y', err.argument)
github ramonhagenaars / jsons / tests / test_validation.py View on Github external
def test_validate_primitive_attribute_invalid(self):
        jsons.set_validator(lambda x: x == 1, int)
        with self.assertRaises(ValidationError):
            jsons.load({'x': 3}, C)
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_load_namedtuple(self):
        dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
                                tzinfo=datetime.timezone.utc)
        T = NamedTuple('T', [('x', str), ('y', datetime.datetime)])
        t = T('test', dat)
        loaded = jsons.load(['test', '2018-07-08T21:34:00Z'], T)
        self.assertEqual(t, loaded)

        T._field_defaults = dict(y=dat)
        loaded2 = jsons.load(['test'], T)
        self.assertEqual(t, loaded)

        with self.assertRaises(UnfulfilledArgumentError):
            jsons.load([], T)
        try:
            jsons.load([], T)
        except UnfulfilledArgumentError as err:
            self.assertEqual([], err.source)
            self.assertEqual(T, err.target)
            self.assertEqual('x', err.argument)