Welcome to AJson’s documentation!¶
How to install it:¶
$ pip install ajson
Description:¶
AJson is a json serializer based on annotations, where you can easily control which and how class attributes should be serialized.
from ajson import AJson
from ajson.aserializer import ASerializer
@AJson()
class Restaurant:
location:str # @aj(groups=["public", "admin"])
tables: int # @aj(groups=["public", "admin"])
owner: str # @aj(groups=["admin"], name=owner_name)
def __init__(self, location, tables, owner):
self.location = location
self.tables = tables
self.owner = owner
serializer = ASerializer()
restaurant = Restaurant("Manhattan", 30, "John Smith")
print(serializer.serialize(restaurant, groups=["public"]))
# {"location": "Manhattan", "tables": 30}
print(serializer.serialize(restaurant, groups=["admin"]))
# {"location": "Manhattan", "tables": 30, "owner_name": "John Smith"}
Or unserialize.
from ajson import AJson
from ajson.aserializer import ASerializer
@AJson()
class Customer:
name: str # @aj(name=firstName)
last_name: str # @aj(name=lastName)
serializer = ASerializer()
serialize_str = '{"firstName": "John", "lastName": "Smith"}'
customer = ASerializer().unserialize(serialize_str, Customer)
print(customer.name) # "John"
print(customer.last_name) # "Smith"
# Validate jsons
# Not passing the required attribute "name
serialize_str = '{"lastName": "Smith"}'
customer = ASerializer().unserialize(serialize_str, Customer)
# this will raise a `AJsonEmptyRequiredAttributeError` Exception