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_json_encoding_error(): class FooBar: pass with pytest.raises(JSONEncodeError): encode(FooBar())
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_encode_non_standard_object_to_json(): assert encode({1, 2, 3}) == {'@': 'set', 'data': [1, 2, 3]}