Example #1
0
    def test_encoding_methods(self):
        converter = JsonConverter()

        def encoder(obj):
            return {"a": [val * 2 for val in obj.a]}

        def decoder(obj):
            obj["a"] = [val // 2 for val in obj["a"]]
            return A(**obj)

        converter.register(A, encoder=encoder, decoder=decoder)
        a = A([1, 2, 3])
        res = converter.dumps(a)
        res_dict = json.loads(res)

        self.assertEqual(res_dict, {
            "__id__": "pelita.test.A",
            "__value__": {
                "a": [2, 4, 6]
            }
        })

        reencoded = converter.loads(res)

        self.assertEqual(a, reencoded)
Example #2
0
    def test_autoregistration_implicit(self):
        json_converter = JsonConverter()

        @json_converter.serializable
        class Autoserialize(object):
            def __init__(self, attr):
                self.attr = attr

            def _to_json_dict(self):
                return {"attr": self.attr}

            @classmethod
            def _from_json_dict(cls, item):
                return cls(**item)

            def __eq__(self, other):
                return self.attr == other.attr

        self.assertEqual(Autoserialize._json_id, "test_json.Autoserialize")

        autoserialize = Autoserialize("an attr")
        converted = json_converter.dumps(autoserialize)
        reencoded = json_converter.loads(converted)
        self.assertEqual(autoserialize, reencoded)

        res_dict = json.loads(converted)
        self.assertEqual(res_dict, {
            "__id__": "test_json.Autoserialize",
            "__value__": {
                "attr": "an attr"
            }
        })
Example #3
0
    def test_autoregistration_implicit(self):
        json_converter = JsonConverter()

        @json_converter.serializable
        class Autoserialize(object):
            def __init__(self, attr):
                self.attr = attr

            def _to_json_dict(self):
                return {"attr": self.attr}

            @classmethod
            def _from_json_dict(cls, item):
                return cls(**item)

            def __eq__(self, other):
                return self.attr == other.attr

        self.assertEqual(Autoserialize._json_id, "test_json.Autoserialize")

        autoserialize = Autoserialize("an attr")
        converted = json_converter.dumps(autoserialize)
        reencoded = json_converter.loads(converted)
        self.assertEqual(autoserialize, reencoded)

        res_dict = json.loads(converted)
        self.assertEqual(res_dict, {"__id__": "test_json.Autoserialize", "__value__": {"attr": "an attr"}})
Example #4
0
    def test_can_encode(self):
        converter = JsonConverter()
        converter.register(A)

        a = A(['1', '2', '3'])
        encoded = converter.dumps(a)

        should_be = {"__id__": "pelita.test.A", "__value__": {"a": ["1", "2", "3"]}}
        decoded = json.loads(encoded)
        self.assertEqual(decoded, should_be)
Example #5
0
    def test_can_recode_nested(self):
        converter = JsonConverter()
        converter.register(A, encoder=A._to_json_dict, decoder=A._from_json_dict)
        converter.register(B)

        a1 = A(1)
        a2 = A("2")
        a3 = A(['1', '2', '3'])
        bb = B("B", a1, a2, a3)

        dumped = converter.dumps(bb)
        loaded = converter.loads(dumped)

        self.assertEqual(bb, loaded)
Example #6
0
    def test_can_encode(self):
        converter = JsonConverter()
        converter.register(A)

        a = A(['1', '2', '3'])
        encoded = converter.dumps(a)

        should_be = {
            "__id__": "pelita.test.A",
            "__value__": {
                "a": ["1", "2", "3"]
            }
        }
        decoded = json.loads(encoded)
        self.assertEqual(decoded, should_be)
Example #7
0
    def test_can_recode_nested(self):
        converter = JsonConverter()
        converter.register(A,
                           encoder=A._to_json_dict,
                           decoder=A._from_json_dict)
        converter.register(B)

        a1 = A(1)
        a2 = A("2")
        a3 = A(['1', '2', '3'])
        bb = B("B", a1, a2, a3)

        dumped = converter.dumps(bb)
        loaded = converter.loads(dumped)

        self.assertEqual(bb, loaded)
Example #8
0
    def test_encoding_methods(self):
        converter = JsonConverter()

        def encoder(obj):
            return {"a": [val * 2 for val in obj.a]}

        def decoder(obj):
            obj["a"] = [val // 2 for val in obj["a"]]
            return A(**obj)

        converter.register(A, encoder=encoder, decoder=decoder)
        a = A([1, 2, 3])
        res = converter.dumps(a)
        res_dict = json.loads(res)

        self.assertEqual(res_dict, {"__id__": "pelita.test.A", "__value__": {"a": [2, 4, 6]}})

        reencoded = converter.loads(res)

        self.assertEqual(a, reencoded)