def test_typemap(): typemap = { Path: ( lambda value: str(value).lstrip('/'), lambda data: Path('/' + data.lstrip('/')), ), } assert dumps(Path('/foo/bar'), typemap) == '"foo/bar"' assert loads(Path, '"foo/bar"', typemap) == Path('/foo/bar')
def test_obj(): class Foo: def __init__(self, uuid, path, dt): self.path = path self.uuid = uuid self.dt = dt def __jsondump__(self): return dump(self.__dict__) @classmethod def __jsonload__(cls, data): return cls( dt=load(datetime, data['dt']), path=load(Path, data['path']), uuid=load(UUID, data['uuid']), ) obj = Foo(uuid4(), Path('/bar'), datetime.now()) result = loads(Foo, dumps(obj)) assert obj.path == result.path assert obj.uuid == result.uuid assert obj.dt == result.dt
def test_moar_nesting(): obj = YourClassList([YourClass(), YourClass()]) encoded = dumps(obj) decoded = loads(YourClassList, encoded) assert obj[0] == decoded[0] assert obj[1] == decoded[1]
def test_uuid(): obj = uuid4() encoded = dumps(obj) decoded = loads(UUID, encoded) assert obj == decoded
def test_your_class_and_nesting(): obj = YourClass() encoded = dumps(obj) decoded = loads(YourClass, encoded) assert obj == decoded
def test_decimal(): obj = Decimal('3.14') assert loads(Decimal, dumps(obj)) == obj
def test_path(): obj = Path('/foo') assert loads(Path, dumps(obj)) == obj
def test_datetime(): obj = datetime.now() assert loads(datetime, dumps(obj)) == obj