Python ObjectJSObject序列化程序包
python-serializer的Python项目详细描述
介绍Python数据契约
有了这个包,您可以严格定义自己的数据契约,并定义data validations和预处理步骤,这些步骤将确保数据的完整性,并能够在JSON格式之间序列化。在
示例用法
from serializer import Interface
class IP(Interface):
# Declaring data type validations
ip_address = str
host_name = str
class Record(Interface):
user_name = str
age = int
ip = IP
# Pre-processors to standardize data
def __user_name__(value):
return value.capitalize()
args = {
'user_name': 'ximi',
'age': 21,
'ip': IP(ip_address='192.168.2.1', host_name='localhost')
}
request = Record(**args)
print ("Serializing from Object to JSON: \n", request.serialize())
# Serializing from Object to JSON:
# {'user_name': 'Ximi', 'age': 21, 'ip': {'ip_address':'192.168.2.1', 'host_name': 'localhost'}}
args = {
'user_name': 'ximi',
'age': 21,
'ip': {
'ip_address': '192.168.2.1',
'host_name': 'localhost'
}
}
request = Record(**args)
print ("Serialise with Reverse type conversion: \n", request.serialize())
# Serialise with Reverse type conversion:
# {'user_name': 'Ximi', 'age': 21, 'ip': {'ip_address': '192.168.2.1', 'host_name': 'localhost'}}
- 项目
标签: