Esempio n. 1
0
    def test_advanced_dumps(self):
        sample = UserList([
            False, 123,
            UserDict({}), 5 + 5j,
            UserDict({
                'haha': 2 + 3j,
                'toto': [False, {
                    'nini': 'xixi'
                }]
            }), True
        ])
        self.assertEqual(
            '[\r\n    false,\r\n    123,\r\n    {},\r\n    {\r\n        "real": 5.0,\r\n       '
            ' "imag": 5.0\r\n    },\r\n    {\r\n        "haha": {\r\n            "real": 2.0,\r\n       '
            '     "imag": 3.0\r\n        },\r\n        "toto": [\r\n            false,\r\n           '
            ' {\r\n                "nini": "xixi"\r\n            }\r\n        ]\r\n    },\r\n  '
            '  true\r\n]', sj.dumps(sample, cls=sj.AdvancedEncoder, indent=4))

        sample = UserList([
            False,
            datetime.now().date(), 123,
            UserDict({}), 5 + 5j,
            UserDict({
                'haha': 2 + 3j,
                'toto': [False, {
                    'nini': 'xixi'
                }]
            }), True
        ])
        haha = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=4)
        print(haha)
        hhh = sj.loads(haha, cls=sj.AdvancedDecoder)
        print(hhh)
Esempio n. 2
0
 def test_custom(self):
     toto = Student('toto')
     self.assertTrue(isinstance(toto, Student))
     a = sj.dumps(toto)
     py_a = sj.loads(a, clazz=Student)
     a2 = sj.dumps(py_a)
     self.assertEqual(a, a2)
Esempio n. 3
0
 def test_advanced_complex(self):
     sample = UserList([
         False, 123,
         UserDict({}), 5 + 5j,
         UserDict({
             'haha': 2 + 3j,
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }), True
     ])
     output1 = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=2)
     sample = deque([
         False, 123,
         UserDict({}), 5 + 5j,
         UserDict({
             'haha': 2 + 3j,
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }), True
     ])
     output2 = sj.dumps(sample, cls=sj.AdvancedEncoder, indent=2)
     self.assertEqual(output1, output2)
     output3 = sj.loads(output1, cls=sj.AdvancedDecoder)
     print(output3)
     self.assertEqual([
         False, 123, {}, (5 + 5j), {
             'haha': (2 + 3j),
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }, True
     ], output3)
     self.assertTrue(isinstance(output3[3], complex))
Esempio n. 4
0
 def test_advanced_slice(self):
     _slice = slice(1, 5, 2)
     sample = deque([
         False, _slice, 123,
         UserDict({}), 5 + 5j,
         UserDict({
             'haha': 2 + 3j,
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }), True
     ])
     output = sj.dumps(sample, cls=sj.AdvancedEncoder)
     print(output)
     self.assertEqual(
         '[false, {"start": 1, "stop": 5, "step": 2}, 123, {}, {"real": 5.0, "imag": 5.0}, {"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, {"nini": "xixi"}]}, true]',
         output)
     output2 = sj.loads(output, cls=sj.AdvancedDecoder)
     print(output2)
     self.assertEqual([
         False, _slice, 123, {}, (5 + 5j), {
             'haha': (2 + 3j),
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }, True
     ], output2)
Esempio n. 5
0
 def test_advanced_uuid(self):
     _uuid = uuid.uuid1()
     sample = deque([
         False, _uuid, 123,
         UserDict({}), 5 + 5j,
         UserDict({
             'haha': 2 + 3j,
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }), True
     ])
     output = sj.dumps(sample, cls=sj.AdvancedEncoder)
     print(output)
     self.assertEqual(
         '[false, "%s", 123, {}, {"real": 5.0, "imag": 5.0}, {"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, {"nini": "xixi"}]}, true]'
         % _uuid, output)
     output2 = sj.loads(output, cls=sj.AdvancedDecoder)
     print(output2)
     self.assertEqual([
         False, _uuid, 123, {}, (5 + 5j), {
             'haha': (2 + 3j),
             'toto': [False, {
                 'nini': 'xixi'
             }]
         }, True
     ], output2)
Esempio n. 6
0
 def test_advanced_datetime(self):
     dt = datetime(year=2017,
                   month=11,
                   day=20,
                   hour=10,
                   minute=53,
                   second=22,
                   tzinfo=timezone(timedelta(-1, 68400)))
     b = UserDict({'haha': 2 + 3j, 'toto': [False, dt]})
     c = sj.dumps(b, cls=sj.AdvancedEncoder)
     self.assertEqual(
         '{"haha": {"real": 2.0, "imag": 3.0}, "toto": [false, "2017-11-20T10:53:22-05:00"]}',
         c)
     print(c)
     d = sj.loads(c, cls=sj.AdvancedDecoder)
     self.assertEqual(
         {
             'haha': (2 + 3j),
             'toto': [
                 False,
                 datetime(2017,
                          11,
                          20,
                          10,
                          53,
                          22,
                          tzinfo=timezone(timedelta(-1, 68400)))
             ]
         }, d)
     print(d)
Esempio n. 7
0
 def test_dumps_dict(self):
     samples = {
         '{}': {},
         '{"abc": true}': {
             "abc": True
         },
         '{"def": [false]}': {
             "def": [False]
         }
     }
     for k, v in samples.items():
         self.assertEqual(k, sj.dumps(v))
Esempio n. 8
0
    def test_dumps_indented(self):
        sample0 = {
            "def":
            [False, {
                'haha': 123,
                "toto": [123, 45, None, [False, True]]
            }],
            '123': None,
            '4334': []
        }
        sample1 = [
            False, {
                'haha': 123,
                "toto": [123, 45, None, [False, True]]
            }
        ]
        sample2 = [
            False, {
                'haha': 123,
                'toto': [False, {
                    'nini': 'xixi'
                }]
            }, True
        ]
        sample3 = {'n1': False, 'n2': {'n3': None, 'n4': True}, 'nn': 'tsdf'}
        sample4 = [{}, {}]
        sample5 = [
            False, {
                'haha':
                123,
                'toto': [
                    False, {
                        'nini':
                        'xixi',
                        'lala': [
                            False, {
                                'haha': 123,
                                "toto": [123, 45, None, [False, True]]
                            }
                        ]
                    }
                ]
            }, True
        ]

        self.assertEqual(
            '{\r\n    "def": [\r\n        false,\r\n        {\r\n            "haha": 123,\r\n            '
            '"toto": [\r\n                123,\r\n                45,\r\n                null,\r\n        '
            '        [\r\n                    false,\r\n                    true\r\n                ]\r\n '
            '           ]\r\n        }\r\n    ],\r\n    "123": null,\r\n    '
            '"4334": []\r\n}', sj.dumps(sample0, indent=4))
        self.assertEqual(
            '[\r\n    false,\r\n    {\r\n        "haha": 123,\r\n        "toto": [\r\n            123,\r\n'
            '            45,\r\n            null,\r\n            [\r\n                false,\r\n          '
            '      true\r\n            ]\r\n        ]\r\n    }\r\n]',
            sj.dumps(sample1, indent=4))
        self.assertEqual(
            '[\r\n    false,\r\n    {\r\n        "haha": 123,\r\n        "toto": [\r\n            '
            'false,\r\n            {\r\n                "nini": "xixi"\r\n            }\r\n        ]\r\n  '
            '  },\r\n    true\r\n]', sj.dumps(sample2, indent=4))
        self.assertEqual(
            '{\r\n    "n1": false,\r\n    "n2": {\r\n        "n3": null,\r\n        "n4": true\r\n   '
            ' },\r\n    "nn": "tsdf"\r\n}', sj.dumps(sample3, indent=4))
        self.assertEqual('[\r\n    {},\r\n    {}\r\n]',
                         sj.dumps(sample4, indent=4))

        self.assertEqual(
            '[\r\n    false,\r\n    {\r\n        "haha": 123,\r\n        "toto": [\r\n           '
            ' false,\r\n            {\r\n                "nini": "xixi",\r\n                "lala": [\r\n'
            '                    false,\r\n                    {\r\n                       '
            ' "haha": 123,\r\n                        "toto": [\r\n                            123,\r\n   '
            '                         45,\r\n                            null,\r\n                        '
            '    [\r\n                                false,\r\n                                true\r\n  '
            '                          ]\r\n                        ]\r\n                    }\r\n        '
            '        ]\r\n            }\r\n        ]\r\n    },\r\n    true\r\n]',
            sj.dumps(sample5, indent=4))
Esempio n. 9
0
 def test_dumps_list(self):
     samples = {'[]': [], '["abc", true]': ['abc', True]}
     for k, v in samples.items():
         self.assertEqual(k, sj.dumps(v))
Esempio n. 10
0
 def test_dumps_string(self):
     sample = {'"123"': '123', '"123\\n456"': '123\n456'}
     for k, v in sample.items():
         self.assertEqual(k, sj.dumps(v))
Esempio n. 11
0
 def test_dumps_number(self):
     samples = {'123': 123, '-123': -123, '0.123': 0.123, '0': 0}
     for k, v in samples.items():
         self.assertEqual(k, sj.dumps(v))
Esempio n. 12
0
 def test_dumps_bool(self):
     samples = {'true': True, 'false': False}
     for k, v in samples.items():
         self.assertEqual(k, sj.dumps(v))
Esempio n. 13
0
 def test_dumps_null(self):
     sample = None
     self.assertEqual('null', sj.dumps(sample))