def test_dumps_serializable(date_with_tz, s_obj): d = dumps(s_obj) assert isinstance(d, str) assert "instance of serializable class" in d assert "instance of child class" in d assert "instance of grand child class" in d assert "2019-08-11T06:10:24.000000+09:00" in d
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_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_from_json_list(date_with_tz, s_obj): o0 = deepcopy(s_obj) o0.strvalue = "instance of serializable class 0" o1 = deepcopy(s_obj) o1.strvalue = "instance of serializable class 1" o2 = deepcopy(s_obj) o2.strvalue = "instance of serializable class 2" d = dumps([o0, o1, o2]) obj = SerializableClass.from_json(d)[1] assert obj.strvalue == "instance of serializable class 1" 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 and datetime are 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 and datetime are 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_dumps_plain_none(): s = dumps(None) assert s == ""
def test_dumps_plain(date_with_tz, p_obj): d = dumps(p_obj) assert isinstance(d, str) assert "instance of plain class" in d assert "2019-08-11T06:10:24.000000+09:00" in d