def test_loads_plain_to_dict(date_with_tz, p_obj): s = dumps(p_obj) d = loads(s) assert d["strvalue"] == "instance of plain class" assert d["intvalue"] == 1 assert d["dtvalue"] == date_with_tz assert d["listvalue"] == [ "list_str_1", 1, date_with_tz, dumpd(p_obj.listvalue[3])] assert d["dictvalue"]["key1"] == "value1" assert d["dictvalue"]["key2"] == 1 assert d["objvalue"]["strvalue"] == "instance of child class"
def test_loads_plain(date_with_tz, p_obj): d = dumps(p_obj) obj = loads(d, PlainClass) assert obj.strvalue == "instance of plain class" assert obj.intvalue == 1 assert obj.dtvalue == date_with_tz assert obj.listvalue == [ "list_str_1", 1, date_with_tz, dumpd(p_obj.listvalue[3])] assert obj.dictvalue["key1"] == "value1" assert obj.dictvalue["key2"] == 1 assert obj.objvalue["strvalue"] == "instance of child class"
def test_loads_with_initargs(date_with_tz): class MyClass: def __init__(self, foo): self.foo = foo self.bar = None @classmethod def create_object(cls, d): obj = cls(d["foo"]) del d["foo"] return obj obj = loads('{"foo": "foo1", "bar": "bar2"}', MyClass) assert obj.foo == "foo1" # set at __init__ assert obj.bar == "bar2" # set by dict value
def test_loads_serializable(date_with_tz, s_obj): d = dumps(s_obj) obj = loads(d, SerializableClass) assert obj.strvalue == "instance of serializable class" assert obj.intvalue == 1 assert obj.dtvalue == date_with_tz assert obj.listvalue[0:2] == ["list_str_1", 1] # when a item of list, object is not restored obj.listvalue[2] = SerializableClass.from_dict(obj.listvalue[2]) assert obj.listvalue[2].to_dict() == s_obj.listvalue[2].to_dict() assert obj.dictvalue["key1"] == "value1" assert obj.dictvalue["key2"] == 1 # when a item of dict, object is not restored obj.dictvalue["key3"] = SerializableClass.from_dict(obj.dictvalue["key3"]) assert obj.dictvalue["key3"].to_dict() == s_obj.listvalue[2].to_dict() # top level member is restored to object by configure `_types` assert obj.objvalue.to_dict() == s_obj.objvalue.to_dict()
def test_example(): class Location: def __init__(self, name=None, school=None): self.name = name self.school = school class Character: def __init__(self, name=None, actor=None): self.name = name self.actor = actor class IdolGroup(Serializable): def __init__(self, name=None, location=None, members=None): self.name = name self.location = location self.members = members # `_types` provides the type to deserialize each members @classmethod def _types(cls): return { "location": Location, "members": Character } idol_group = IdolGroup( name="μ’s", location=Location("Akihabara", "Otonokizaka High"), members=[Character("Honoka Kosaka", "Emi Nitta"), Character("Umi Sonoda", "Suzuko Mimori"), Character("Kotori Minami", "Aya Uchida")]) # encode to json s = idol_group.to_json(indent=2, ensure_ascii=False) # decode to object with members also decoded obj = IdolGroup.from_json(s) assert obj.name == "μ’s" assert obj.location.name == "Akihabara" assert obj.location.school == "Otonokizaka High" assert obj.members[2].name == "Kotori Minami" clist = loads(dumps(idol_group.members), Character) assert clist[1].name == "Umi Sonoda"
def test_loads_plain_none(): obj = loads("") assert obj is None obj = loads(None) assert obj is None