Interoperates @dataclass with json objects
Project description
jsondaora
Interoperates dataclasses and TypedDict annotations with json objects for python
Documentation: https://dutradda.github.io/jsondaora
Source Code: https://github.com/dutradda/jsondaora
Key Features
- Full compatibility with dataclasses module and TypedDict annotation
- Deserialize values from dict
- Deserialize values from bytes*
- Deserialization/serialization of choosen fields
- Dict serialization
- Direct json serialization with orjson (don't convert to dict recursively before serialization)
- Optional validation according with the json-schema specification*
* feature in development.
Requirements
- Python 3.8+
- orjson for json serialization
Instalation
$ pip install jsondaora
Basic example
from dataclasses import dataclass
from typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
# dataclass
@dataclass
class Music:
name: str
# if 'Person' is not a dataclass the
# 'jsondaora' decorator will call the
# 'dataclass' decorator
@jsondaora
class Person:
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = asdataclass(jsondict, Person)
print('dataclass:')
print(person)
print(dataclass_asjson(person))
print()
# TypedDict
@jsondaora
class Music(TypedDict):
name: str
# This decorator is required because
# we need to track the annotations
@jsondaora
class Person(TypedDict):
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, Person)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, Person))
dataclass:
Person(name='John', age=40, musics=[Music(name='Imagine')])
b'{"name":"John","age":40,"musics":[{"name":"Imagine"}]}'
TypedDict:
{'name': 'John', 'age': 40, 'musics': [{'name': 'Imagine'}]}
b'{"name":"John","age":40,"musics":[{"name":"Imagine"}]}'
Example for choose fields to deserialize
from dataclasses import dataclass
from typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
@dataclass
class Music:
name: str
@jsondaora(deserialize_fields=('name'))
class Person:
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = asdataclass(jsondict, Person)
print('dataclass:')
print(person)
print(dataclass_asjson(person))
print()
# TypedDict
@jsondaora
class Music(TypedDict):
name: str
@jsondaora(deserialize_fields=('name'))
class Person(TypedDict):
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, Person)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, Person))
dataclass:
Person(name='John', age='40', musics=[{'name': 'Imagine'}])
b'{"name":"John","age":"40","musics":[{"name":"Imagine"}]}'
TypedDict:
{'name': 'John', 'musics': [{'name': 'Imagine'}], 'age': '40'}
b'{"name":"John","musics":[{"name":"Imagine"}],"age":"40"}'
Example for choose fields to serialize
from dataclasses import dataclass
from typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
@dataclass
class Music:
name: str
@jsondaora(serialize_fields=('name', 'age'))
@dataclass
class Person:
name: str
age: int
musics: List[Music]
jsondict = dict(name='John', age=40, musics=[dict(name='Imagine')])
person = asdataclass(jsondict, Person)
print('dataclass:')
print(person)
print(dataclass_asjson(person))
print()
# TypedDict
@jsondaora
class Music(TypedDict):
name: str
@jsondaora(serialize_fields=('age'))
class Person(TypedDict):
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, Person)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, Person))
dataclass:
Person(name='John', age=40, musics=[Music(name='Imagine')])
b'{"age":40,"name":"John"}'
TypedDict:
{'name': 'John', 'age': 40, 'musics': [{'name': 'Imagine'}]}
b'{"age":40}'
Wins Pydantic Benchmark
jsondaora
is up to 3.2 times faster than pydantic on it's benchmark
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
jsondaora-0.4.0.tar.gz
(72.9 kB
view hashes)
Built Distribution
jsondaora-0.4.0-py3-none-any.whl
(34.0 kB
view hashes)
Close
Hashes for jsondaora-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d46a4a018920d954321a75e9241e56e48b69e15bf07830a2f1d47a94778b9bd8 |
|
MD5 | 47cb23873cd28495b1d131642c264118 |
|
BLAKE2b-256 | ab6ea11d7302c50a6b3e5058c6a65ed2cd16d216504a94aa6d5365ed9c3ea05a |