ObjectJSON文档映射器
podm的Python项目详细描述
被告:(P)伊顿(O)标的-(D)上诉人
这个库的目的是创建一些对象,这些对象可以轻松地序列化到以后可以存储为JSON或YAML的字典。 它的目的是在jsonpickle输出格式根本不好的情况下替代jsonpickle。在
特点:
- 支持有序字典或自定义映射类
- 自定义字段序列化/反序列化
- 验证器
- Json架构生成
安装
pip3 install podm
一些用例示例
^{pr2}$反序列化没有类型信息的字典
data={'company-name':'master','description':'some description'}company=Company.from_dict(data)
用类型信息反序列化字典
使用与jsonpickle相同的字段。在
data={'py/object':'Company','company-name':'master','description':'some description'}company=JsonObject.parse(data)
Jsonpickle格式支持
data={'py/object':'Company','py/state':{'company-name':'master','description':'some description'}}company=JsonObject.parse(data)
自动生成的getter/setters
如果它们被声明为属性,则访问器将使用它们。在
classCompany(JsonObject):company_name=Property('company-name')# Specify a different field name in json.# Automatically generated gettercompany_name=company.get_company_name()# Also property accessorscompany_name=company.company_name# And private attributescompany_name=company._company_name
编写自定义的getter和setter
classCompany(JsonObject):company_name=Property('company-name')# Specify a different field name in json.defget_company_name(self):print('Getter called!!!')# generated attributereturnself._company_name# So, when calling the property getter ...company_name=company.company_name# will print 'Getter called!!!'
支持订单EDDICT
serialized=company.to_dict(OrderedDict)# serialized data is instance of OrderedDictclassTestObject(JsonObject):val1=Property()obj=TestObject()obj.val1=OrderedDict(key1='value1')serialized=company.to_dict()# serialized will be instance of dict, field 'val1' will be instance of OrderedDict
枚举支持
可以决定如何序列化/反序列化枚举。在
classInvoiceType(Enum):TYPE_A=1TYPE_B=2classInvoice(JsonObject):invoice_type=Property(type=InvoiceType,enum_as_str=True)invoice=Invoice(invoice_type=InvoiceType.TYPE_A)serialized=invoice.to_dict()print(serialized['invice_type'])# Will print 'TYPE_A'classInvoice(JsonObject):invoice_type=Property(type=InvoiceType)invoice=Invoice(invoice_type=InvoiceType.TYPE_A)serializd=invoice.to_dict()print(serialized['invoice_type'])# Will print 1
Json架构生成。
检查测试用例中的示例。在
验证器。
检查测试用例中的示例。在
- 项目
标签: