Skip to main content

simple serializer based on annotations

Project description

# AJson (Annotations Json Serializer)

AJson is a serializer based on annotations that gives a lot of flexibility and configuration for you serialization process.

[![Build Status](https://travis-ci.org/JorgeGarciaIrazabal/ajson.svg?branch=master)](https://travis-ci.org/JorgeGarciaIrazabal/ajson)
[![codecov](https://codecov.io/gh/JorgeGarciaIrazabal/ajson/branch/master/graph/badge.svg)](https://codecov.io/gh/JorgeGarciaIrazabal/ajson)


### Install: (python3.6 or greater)

`pip install ajson`

#### Motivation:

There are amazing serialization libraries like [jsonpickle](https://jsonpickle.github.io/), and even more when the serialized object is meant to be used in python too.
But there are no libraries that let you filter the fields to serialize or modify the names of the attributes, which are features super useful, mainly for http APIs

This library allows you to have those features in a simple and intuitive way.

#### Serialize Examples

###### Simple Serialization With "Groups"
If you want to filter some sensible data in some scenarios, you can define `groups` per each attribute to control what is serialize and what is not

```python
from ajson import AJson
from ajson.aserializer import ASerializer

@AJson()
class Restaurant:
def __init__(self):
self.location = "Manhattan" # @aj{"groups": ["public", "admin"]}
self.tables = 30 # @aj{"groups": ["public", "admin"]}
self.owner = "John Smith" # @aj{"groups": ["admin"]}

restaurant = Restaurant()
print(ASerializer().serialize(restaurant, groups=["public"])) # {"location": "Manhattan", "tables": 30}
print(ASerializer().serialize(restaurant, groups=["admin"])) # {"location": "Manhattan", "tables": 30, "owner": "John Smith"}
```

###### Rename Attributes With "Name"

```python
from ajson import AJson
from ajson.aserializer import ASerializer

@AJson()
class Customer:
name: str # @aj{"name": "firstName"}
primary_email: str # @aj{"name": "email"}
last_name: str # @aj{"name": "lastName"}
def __init__(self):
self.name = "John"
self.last_name = "Smith"
self.primary_email = "john.smith@something.com"

customer = Customer()
print(ASerializer().serialize(customer))
# {"firstName": "John", "lastName": "Smith", "email": "john.smith@something.com"}
```

###### Nested Objects With Groups And Names

```python
from ajson import AJson
from ajson.aserializer import ASerializer

@AJson()
class Customer:
def __init__(self, name, primary_email):
self.name = name # @aj{"name": "firstName", "groups": ["public"]}
self.primary_email = primary_email
'''
You can also add the annotation in a multiline docstr
@aj{
"name": "email",
"groups": ["admin"]
}
'''

@AJson()
class Restaurant:
def __init__(self):
self.location = None # @aj{"groups": ["public", "admin"]}
self.owner = "John Smith" # @aj{"groups": ["admin"]}
self.customer_list = [ # @aj{"groups": ["with_customers"], "name": "customers"}
Customer("Dani", "dani@something.com"),
Customer("Mike", "maki@something.com")
]

restaurant = Restaurant()
print(ASerializer().serialize(restaurant, groups=["public"]))
# {"location": null, "tables": 30}

# if you want to get the dictionary instead of a string, you can call `to_dict` instead of `serialize`
print(ASerializer().to_dict(restaurant, groups=["public", "with_customers"]))
'''
{
"location": "Spain",
"customers": [
{"firstName": "Dani", "email": "dani@something.com"},
{"firstName": "Mike", "email": "maki@something.com"}
]
}
'''
```

#### Unserialize Examples

###### UnSerialization With Custom Names
```python
from ajson import AJson
from ajson.aserializer import ASerializer

@AJson()
class Customer:
name: str # @aj{"name": "firstName"}
primary_email: str # @aj{"name": "email"}
last_name: str # @aj{"name": "lastName"}

serialize_str = '{"firstName": "John", "lastName": "Smith", "email": "john.smith@something.com"}'
customer = ASerializer().unserialize(serialize_str, Customer)
print(customer.name) # "John"
print(customer.last_name) # "Smith"
print(customer.primary_email) # "john.smith@something.com"
```

###### Nested Objects

```python
from typing import List
from ajson import AJson
from ajson.aserializer import ASerializer


@AJson()
class Customer:
def __init__(self):
self.name = None # @aj{"name": "firstName"}
self.primary_email = None # @aj{"name": "email"}

@AJson()
class Restaurant:
customer_list: List[Customer] # if we want to have nested objects, we need to define the types with the annotations
'''
@aj{"name": "customers"}
we can create the @aj annotation in the attribute definition
'''
owner: str
location: str

def __init__(self):
self.location = None
self.owner = "John Smith"
self.customer_list = []

restaurant_str = '''
{
"location": "Spain",
"customers": [
{"firstName": "Dani", "email": "dani@something.com"},
{"firstName": "Mike", "email": "maki@something.com"}
]
}
'''

restaurant = ASerializer().unserialize(restaurant_str, Restaurant)
print(restaurant.owner) # "John Smith"
print(restaurant.customer_list[0].name) # "Dani"
```



Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ajson-0.6.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ajson-0.6.0-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file ajson-0.6.0.tar.gz.

File metadata

  • Download URL: ajson-0.6.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for ajson-0.6.0.tar.gz
Algorithm Hash digest
SHA256 0a38ffe6e179835ec613fcb925e2abc6b9db102c5718307400125749ffbedbbd
MD5 d97038d872a8198a5a07f5dbf33a3d43
BLAKE2b-256 1c9e46dc2eff537347a81f970bff7b1d86baa110b7064e5a2e52bb6d7bb13799

See more details on using hashes here.

File details

Details for the file ajson-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: ajson-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for ajson-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 63fec609cf18f77af9f1f5f3e31bdebc9ff8fe0b47cd80b4671002b2b490244a
MD5 624d45c98021cba03ce3117d87e0980c
BLAKE2b-256 bd648f70304b1123c18c6bfa93a5cc2ef6bd9ceec23d92281219283633acd5c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page