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
def test_round_trip(elem): assert decode(encode(elem)) == elem
def test_dict_encode(): D = {'foo': 1, 'bar': 42} assert decode(encode(D)) == D
def test_decode_non_standard_object_to_json(): assert decode({'@': 'set', 'data': [1, 2, 3]}) == {1, 2, 3}
def test_json_decoding_error(): with pytest.raises(JSONDecodeError): decode({'@': 'mod.FooBar', 'x': 1})