Example #1
0
 def test_arrays(self):
     a = []
     b = [1, 2, 3]
     c = ['a', b, []]
     self.assertEqual(to_json(a), '[]')
     self.assertEqual(to_json(b), '[1,2,3]')
     self.assertEqual(to_json(c), '["a",[1,2,3],[]]')
Example #2
0
def main():
    obj = [
        {
            "a": 1,
            "b": [
                [
                    [
                        1,
                        2,
                        3,
                        [
                            [
                                5,
                                6,
                                [False, [True], [{}, {}, {}, None, False, True, 10.12, -10.13123123]],
                                {"a": {"b": {"c": {"d": [-1, -2, -3, 0]}}}},
                            ]
                        ],
                    ]
                ]
            ],
        },
        {},
        {},
        False,
        "sample text",
        {"!": [{}, [{'"': "this \t is \n quote key"}, {}]]},
    ]
    json = to_json(obj)
    parsed = from_json(json)
    print(json)
    print(parsed)
    print("obj == parsed is", parsed == obj)
Example #3
0
def main():
    class A(object):
        def __init__(self, array):
            self.a = {"a": array}
            self.b = "str"
            self.c = 10

    class B(object):
        def __init__(self):
            self.field = A([1, 2, 3])

    class C(A, B):
        def __init__(self):
            A.__init__(self, ['A', 'B'])
            B.__init__(self)
            self.complex_class = [A(['a', 'b', 'c']), B()]

    print('int:', to_json(1))
    print('float:', to_json(3.14))
    print('str:', to_json('1'))
    print('list:', to_json([1, 2, 3, ]))
    print('tuple:', to_json(('a', 'b', 'c')))
    print('dict:', to_json({'a': 1, 'b': [1, 2, 3], "c": {}, "d": {"e": 1}, "f": {"flag": True, "null": None}}))
    print('complex object:', to_json(C()))
Example #4
0
 def test_equal(self):
     obj = [{'a': 1, 'b': [
         [[1, 2, 3, [[5, 6, [False, [True], [{}, {}, {}, None, False, True, 10.12, -10.13123123]],
                      {'a': {'b': {'c': {'d': [-1, -2, -3, 0]}}}}]]]]]}, {}, {}, False, "sample text",
            {'!': [{}, [{'"': 'this \t is \n quote key'}, {}]]}]
     self.assertSequenceEqual(from_json(to_json(obj)), obj)
Example #5
0
 def test_notimplemented_error(self):
     try:
         self.assertRaises(NotImplementedError, to_json(range(1, 10)))
     except RuntimeError as e:
         self.assertIsInstance(e, NotImplementedError)
Example #6
0
    def test_class(self):
        class A(object):
            def __init__(self):
                self.a = 10

        self.assertEqual(to_json(A()), '{"a":10}')
Example #7
0
 def test_dict(self):
     obj = {'a': {'b': 10}}
     self.assertEqual(to_json(obj), '{"a":{"b":10}}')
Example #8
0
 def test_values(self):
     self.assertEqual(to_json(False), 'false')
     self.assertEqual(to_json(None), 'null')
     self.assertEqual(to_json(-1.23), '-1.23')