Package: jsonalize
IntroductionThis library supports two-way data binding between JSON and Python class.TurorialHere is a simple example to use jsonalize:fromjsonalizeimport*# Define a classclassMyData(JSONObject):def__init__(self):JSONObject.__init__(self)self.id=JSONString()self.name=JSONString()self.age=JSONInt()self.weight=JSONFloat()# Create an object of MyDatamy=MyData()my.id="20190101"my.name="Stanley"my.age=28my.weight=60# jsonalize the objectjson_str=my.to_json()print(json_str)# restore the object from jsonmy2=JSONObject.from_json(MyData,json_str)print(my2.to_json())This example should output the following message:{"age": 28, "id": "20190101", "weight": 60.0, "name": "Stanley"}
{"age": 28, "id": "20190101", "weight": 60.0, "name": "Stanley"}Another example shows how to prevent creating real objects for attributes when constructing a new JSONObject instance.classMyData(JSONObject):def__init__(self):JSONObject.__init__(self)self.id=JSONNoneIntself.name=JSONNoneStringself.attr=JSONObject.NoneValue(Attr)classAttr(JSONObject):def__init__(self):JSONObject.__init__(self)self.value=JSONString()print("Attr object created")my=MyData()print(my.idisNone,my.nameisNone,my.attrisNone)my.id=10my.name="Stanley"my.attr=Attr()my.attr.value="attr"print(my.to_json())# Outputs:# (True, True, True)# Attr object created# {"attr": {"value": "attr"}, "name": "Stanley", "id": 10}Key points from this tutorialA serializable class should inherit theJSONObjectclassDon't forget to invoke the__init__method in your classThe serializable class attributes should be set asJSON**typesList of supported JSON typesJSONIntJSONLong (Only in Python 2)JSONFloatJSONComplexJSONBoolJSONStringJSONListJSONSetJSONDictJSONObjectMost of the types can be initialized with an initial value, for example:a_string=JSONString("hello jsonalize")Remarks for JSONBoolYou can't test the value of a JSONBool object with theiskeyword, becauseiswill compare the instance id of two objects, but an object of JSONBool is never an instance ofTrueorFalse.You can do the test as follows:a_bool=JSONBool(True)print(a_boolisTrue,a_bool==True)# False, Trueprint(a_bool.true(),a_bool.true()isTrue,a_bool.equals(True))# True, True, TrueA more complex exampleYou can have an object of JSONObject in another JSONObject class:classMonitor(JSONObject):def__init__(self):JSONObject.__init__(self)self.size=JSONFloat()self.power=JSONFloat()self.color=JSONString()classComputer(JSONObject):def__init__(self):JSONObject.__init__(self)self.brand=JSONString()self.monitor=Monitor()computer=Computer()computer.brand="Lenovo"computer.monitor.size=23.0computer.monitor.power=25.0json_str=computer.to_json()print(json_str)#{"brand": "Lenovo", "monitor": {"color": "", "power": 25.0, "size": 23.0}}computer2=JSONObject.from_json(Computer,json_str)print(computer2.to_json())#{"brand": "Lenovo", "monitor": {"color": "", "power": 25.0, "size": 23.0}}A list of JSONObject objects?Look at the following example:classStudent(JSONObject):def__init__(self):JSONObject.__init__(self)self.id=JSONString()self.name=JSONString()classSchool(JSONObject):def__init__(self):JSONObject.__init__(self)self.address=JSONString()self.students=JSONList()stu1=Student()stu1.id="20190202"stu1.name="Stanley"stu2=Student()stu2.id="20190203"stu2.name="Cyrus"school=School()school.address="Central Street No.23"school.students.append(stu1)school.students.append(stu2)json_str=school.to_json()print(json_str)#{"students": [{"id": "20190202", "name": "Stanley"}, {"id": "20190203", "name": "Cyrus"}], "address": "Central Street No.23"}school2=JSONObject.from_json(School,json_str)print(type(school2.students[0]),school2.students[0])#(<type 'dict'>, {u'id': u'20190202', u'name': u'Stanley'})As you can see here, the deserializing of the School object is incorrect. Because any type of object could be appended to school.students, sojsonalizedon't know what to restore when deserializing.This problem is currently not resolved, maybe I can add a type mapping structure in the future.