コード例 #1
0
def test_register_new_type():
    class Foo:
        def __eq__(self, other):
            return self.__dict__ == other.__dict__

    def efunc(x):
        return {'data': dict(x.__dict__)}

    def dfunc(data):
        foo = Foo()
        for k, v in data['data'].items():
            setattr(foo, k, v)
        return foo

    register(Foo, 'mod.Foo', encode=efunc, decode=dfunc)
    foo = Foo()

    assert Foo in bricks.json.encoders.CLASS_TO_NAMES
    assert encode(foo) == {'@': 'mod.Foo', 'data': {}}
    assert decode({'@': 'mod.Foo', 'data': {}}) == foo
コード例 #2
0
def test_json_encoding_error():
    class FooBar:
        pass

    with pytest.raises(JSONEncodeError):
        encode(FooBar())
コード例 #3
0
def test_round_trip(elem):
    assert decode(encode(elem)) == elem
コード例 #4
0
def test_dict_encode():
    D = {'foo': 1, 'bar': 42}
    assert decode(encode(D)) == D
コード例 #5
0
def test_encode_non_standard_object_to_json():
    assert encode({1, 2, 3}) == {'@': 'set', 'data': [1, 2, 3]}