How to use jsons - 10 common examples

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_primitive.py View on Github external
def test_dump_and_cast(self):

        class C:
            def __init__(self, x: int):
                self.x = x

        self.assertEqual(42, jsons.dump('42', int))
        self.assertEqual(42.0, jsons.dump('42', float))
        self.assertEqual('42', jsons.dump(42, str))
        self.assertEqual(True, jsons.dump(42, bool))

        with self.assertRaises(SerializationError):
            jsons.dump('fortytwo', int)

        try:
            jsons.dump('fortytwo', int)
        except SerializationError as err:
            self.assertTrue('fortytwo' in err.message)
github ramonhagenaars / jsons / tests / test_decorator.py View on Github external
def func1(c1, c2: C) -> C:
            self.assertEqual(c1['x'], 'c1')
            self.assertEqual(c2.x, 'c2')
            return jsons.dump(C('c_res'))
github ramonhagenaars / jsons / tests / test_object.py View on Github external
def test_dump_object(self):
        obj = AllDumpable(AllDumpable())
        exp = {'_par_c': 10, 'par_v': None, 'par_p': 12,
               'c': 1, '_c': 2, 'c_n': None, '_c_n': None,
               'child': None, 'v': 3, '_v': 4, 'v_n': None, '_v_n': None,
               'p': 5, '_p': 5, 'p_n': None, '_p_n': None}
        exp['child'] = exp.copy()
        dump = jsons.dump(obj)
        self.assertDictEqual(exp, dump)
github ramonhagenaars / jsons / tests / test_jsonserializable.py View on Github external
f2.set_serializer(lambda *_, **__: 'f2', str)

        class C1(f1):
            def __init__(self):
                self.x = 'some string'

        class C2(f2):
            def __init__(self):
                self.x = 'some string'

        c1 = C1()
        c2 = C2()

        self.assertDictEqual(c1.json, {'x': 'f1'})
        self.assertDictEqual(c2.json, {'x': 'f2'})
        self.assertEqual(jsons.dump(c1.x), 'some string')
        self.assertEqual(jsons.dump(c1.x, fork_inst=f1), 'f1')
        self.assertEqual(jsons.dump(c1.x, fork_inst=f2), 'f2')  # Note: c1.x!
github ramonhagenaars / jsons / tests / test_object.py View on Github external
def test_dump_with_error(self):
        class C:
            @property
            def x(self):
                raise KeyError('Some bug this is!')

        with self.assertRaises(SerializationError):
            jsons.dump(C())
github ramonhagenaars / jsons / tests / test_object.py View on Github external
def test_dump_load_object_verbose(self):
        h = StateHolder()
        dumped = jsons.dump(h, verbose=True)
        loaded = jsons.load(dumped)
        self.assertEqual(type(h), type(loaded))
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_dump_object_verbose(self):
        class A:
            def __init__(self, x):
                self.x = x

        class B:
            def __init__(self, a: A):
                self.a = a

        class C:
            def __init__(self, b: B):
                self.b = b

        c = C(B(A(42)))
        dumped = jsons.dump(c, verbose=True)
        expectation = {
            'classes': {
                '/': 'test_jsons.C',
                '/b': 'test_jsons.B',
                '/b/a': 'test_jsons.A'
            }
        }

        self.assertDictEqual(expectation, dumped['-meta'])
github ramonhagenaars / jsons / tests / test_object.py View on Github external
def test_dump_with_slots(self):
        class C:
            __slots__ = 'x', 'y'

            def __init__(self, x):
                self.x = x
                self.y = 'This is no parameter'

        c = C('something')
        dumped = jsons.dump(c)
        self.assertDictEqual(dumped, {'x': 'something',
                                      'y': 'This is no parameter'})
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_dump_int(self):
        self.assertEqual(123, jsons.dump(123))
github ramonhagenaars / jsons / test_jsons.py View on Github external
def test_dump_as_parent_type(self):
        class Parent:
            __slots__ = ['parent_name']

            def __init__(self, pname):
                self.parent_name = pname

        class Child(Parent):
            def __init__(self, cname, pname):
                Parent.__init__(self, pname)
                self.child_name = cname

        c = Child('John', 'William')
        dumped1 = jsons.dump(c)
        dumped2 = jsons.dump(c, Parent)
        self.assertDictEqual(dumped1, {'child_name': 'John',
                                       'parent_name': 'William'})
        self.assertDictEqual(dumped2, {'parent_name': 'William'})