基于注释的简单序列化程序

ajson的Python项目详细描述


#a json(annotations json serializer)

ajson是一个基于注释的序列化器,它为序列化过程提供了很多灵活性和配置。

[![构建状态](https://travis-ci.org/jorgegarciairazabal/ajson.svg?branch=master)(https://travis ci.org/jorgegarciairazabal/ajson)
[![codecov](https://codecoov.io/gh/jorggegarciagarciaiairarzabaal/ajson/branch/branch/master/graph/badge/graph/badge.svg)(https://codecoov.io/gh/jorggegarciaariariarizabaal/ajson)












人们的动机:






像这样的图书馆[jsonpickle](https://jsonpickle.github.io/),当序列化的对象也要在python中使用时更是如此。
但是没有库允许您筛选字段以序列化或修改属性的名称。这些特性非常有用,主要用于http api

此库允许您以简单直观的方式拥有这些特性。


ine`groups`每个属性控制序列化的内容和不序列化的内容。

``python
来自ajson import ajson,aseriazer

@ajson()
类餐厅:
位置:str@aj(groups=["public","admin"])
表:int@aj(groups=["public","admin"])
所有者:str@aj(groups=["admin"])
def_uu init_u(self,location,tables,owner):
self.location=location
self.tables=tables
self.owner=owner

serializer=aserializer()
restaurant=restaurant("曼哈顿",30,"john smith")
打印(serializer)。序列化(restaurant,groups=["public"])
{"location":"manhattan","tables":30}
打印(serializer.serialize(restaurant,groups=["admin"])
{"location":"manhattan","tables":30,"owner":"john smith"}
```

``python
``f重命名属性rom ajson import ajson
从ajson导入ajson。aserizer import aserizer

@ajson()
类客户:
名称:str@aj(名称=名字)
主电子邮件:str@aj(名称=电子邮件)
姓氏:str@aj(名称=姓氏)
def init_uu(self):
self.name="john"
self.last戋name="smith"
self.primary戋email="john.smith@something.com"


customer=customer()
print(serializer.serialize(customer))
{"firstname":"john","lastname":"smith","email":"john.smith@something.com"}
`````

在多行文档中添加注释str
@aj(
name=email,
groups=["public"]
"
"


def\uu init(self,name,primary\u email):
self.name=name
self.primary\u email=primary\u email

@ajson()
类餐厅:
位置:str@aj(groups=["public","admin"])
所有者:str@aj(groups=["admin"])
客户列表:list[customer]@aj(groups=["with_customers"]name=customers)

定义初始(self):
self.location=none
self.owner="john smith"
self.customer_list=[
customer("dani","dani@something.com"),
customer("mike","maki@something.com")
]


print(aseriazer().serialize(restaurant,groups=["public"])
{"location":null}

要获取字典而不是字符串,可以调用"to_dict"而不是"serialize"
print(aserializer().to_dict(餐厅、团体=["public","with_customers"])
'
{
"location":none,
"customers":[
{"firstname":"dani","email":"dani@something.com"},
{"firstname":"mike","email":"maki@something.com"}
]
'
`````

/>#####使用来自Ajson import Ajson的自定义名称进行非序列化
``python
,aserializer

@Ajson()
类客户:
名称:str@AJ(名称=名字)
主要电子邮件:str@AJ(名称=电子邮件)
姓氏:str@AJ(名称=姓氏)

serializer=aserizer(
序列化str='{"firstname":"john","last name":"smith","email":"john.smith@something.com"}
customer=serializer.unserialize(serialize_str,customer)
打印(customer.name)"john"
打印(customer.last_name)"smith"
打印(customer.primary_email)"john.smith@someth"com"
```

``嵌套对象

``python
``python
乙r/>self.name=none@aj(name=firstname)
self.primary_u email=none@aj(name=email)

@ajson()
类餐厅:
customer_list:list[customer]如果要有嵌套对象,我们需要定义类型提示
'
'@aj(name=customers)
我们可以在属性定义中创建@aj注释
'
所有者:str="john smith"
位置:可选的[str]=none



"dani@something.com"},
{"firstname":"mike","email":"maki@something.com"}
]
}
"


serializer=aserializer()
restaurant=serializer.unserialize(restaurant嫘str,restaurant)
print(restaurant.owner);"john smith"
print(restaurant.customer嫘list[0].name)"dani"
````






1已知限制。不支持使用类型(dict[str:myobject])取消dict的序列化,它只是将其取消序列化为dict。

2。使用不同于字符串的键(dict[int:str])取消dict的序列化

我。

请随意创建任何问题或prs。
BR/>

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用Ibatis在插入时返回ID(使用返回关键字)   java(org.hibernate.TransactionException)org。冬眠TransactionException:事务未成功启动   java小程序jwindow始终位于JNLP顶部   在Java中重新解析JSON对象?   java单击后将ListView数据移动到新屏幕   Mule ESB中的java WSA寻址特性   Java,对象编程:获取返回0值的方法   hibernate的Java通用问题,如何处理T get(K id)   java在使用超级CSV读取CSV时忽略引用   ssh使用Java远程运行命令   java如何向具体用户发送websocket消息?   在JAVA中,我可以在不指定的情况下使用条件运算符吗?