Example #1
0
    def test_to_json(self):
        obj = {
            'a': 1,
            'b': '2',
            'c': [1, 2, ['1', '2', '3'], 3],
        }
        self.assertEqual(from_json(to_json(obj)), obj)

        obj = {
            'a': [1, 2, True],
            'b': {
                'a': None,
                'b': 1.0
            },
            'd': {
                'a': 10,
                'b': {
                    'a': 20
                }
            },
            'c': [{
                'a': False
            }, [1, 2, None], "pipka"],
        }
        self.assertEqual(from_json(to_json(obj)), obj)

        with self.assertRaises(TypeError):
            to_json(set([1, 2, 3, 2, 1]))
Example #2
0
    def test_from_json(self):
        obj = {
            'a': 1,
            'b': 2,
            'c': 3
        }
        json_string = '{"a": 1, "b": 2, "c": 3}'
        self.assertEqual(from_json(json_string), obj)

        obj = {
            'a': 1,
            'b': '2',
            'c': [1, 2, ['1', '2', '3'], 3],
        }
        json_string = '{"a": 1, "b": "2", "c": [1, 2, ["1", "2", "3"], 3]}'
        self.assertEqual(from_json(json_string), obj)

        obj = {
            'a': [1, 2, True],
            'b': {'a': None, 'b': 1.0},
            'c': [{'a': False}, [1, 2, None], "pipka"],
            'd': {'a': 10, 'b': {'a': 20}}
        }
        json_string = """{
            "a": [1, 2, true], "c": [{"a": false}, [1, 2, null], "pipka"]
            "b": {"a": null, "b": 1.0},
            "d": {"a": 10, "b": {"a": 20}}
        }"""
        self.assertEqual(from_json(json_string), obj)

        with self.assertRaises(ValueError):
            print from_json('not json')
Example #3
0
    def test_to_json(self):
        obj = {
            'a': 1,
            'b': '2',
            'c': [1, 2, ['1', '2', '3'], 3],
        }
        self.assertEqual(from_json(to_json(obj)), obj)

        obj = {
            'a': [1, 2, True],
            'b': {'a': None, 'b': 1.0},
            'd': {'a': 10, 'b': {'a': 20}},
            'c': [{'a': False}, [1, 2, None], "pipka"],
        }
        self.assertEqual(from_json(to_json(obj)), obj)

        with self.assertRaises(TypeError):
            to_json(set([1, 2, 3, 2, 1]))
Example #4
0
    def test_from_json(self):
        obj = {'a': 1, 'b': 2, 'c': 3}
        json_string = '{"a": 1, "b": 2, "c": 3}'
        self.assertEqual(from_json(json_string), obj)

        obj = {
            'a': 1,
            'b': '2',
            'c': [1, 2, ['1', '2', '3'], 3],
        }
        json_string = '{"a": 1, "b": "2", "c": [1, 2, ["1", "2", "3"], 3]}'
        self.assertEqual(from_json(json_string), obj)

        obj = {
            'a': [1, 2, True],
            'b': {
                'a': None,
                'b': 1.0
            },
            'c': [{
                'a': False
            }, [1, 2, None], "pipka"],
            'd': {
                'a': 10,
                'b': {
                    'a': 20
                }
            }
        }
        json_string = """{
            "a": [1, 2, true], "c": [{"a": false}, [1, 2, null], "pipka"]
            "b": {"a": null, "b": 1.0},
            "d": {"a": 10, "b": {"a": 20}}
        }"""
        self.assertEqual(from_json(json_string), obj)

        with self.assertRaises(ValueError):
            print from_json('not json')