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 chosen fields
- Deserialize the following standard types: Dict, List, Tuple, Set, Union and Any
- 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
Installation
$ pip install jsondaora
Basic example
from typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
@jsondaora
class Person:
name: str
age: int
class Music:
name: str
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 MusicT(TypedDict):
name: str
@jsondaora
class PersonT(TypedDict):
name: str
age: int
musics: List[MusicT]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, PersonT)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, Person))
dataclass:
Person(name='John', age=40, musics=[Person.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 typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
@jsondaora(deserialize_fields=('name'))
class Person:
name: str
age: int
class Music:
name: str
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 MusicT(TypedDict):
name: str
@jsondaora(deserialize_fields=('name',))
class PersonT(TypedDict):
name: str
age: int
musics: List[MusicT]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, PersonT)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, PersonT))
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 typing import List, TypedDict
from jsondaora import (
as_typed_dict,
asdataclass,
dataclass_asjson,
jsondaora,
typed_dict_asjson,
)
@jsondaora(serialize_fields=('name', 'age'))
class Person:
name: str
age: int
class Music:
name: str
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 PersonT(TypedDict):
name: str
age: int
musics: List[Music]
jsondict = dict(name=b'John', age='40', musics=[dict(name='Imagine')])
person = as_typed_dict(jsondict, PersonT)
print('TypedDict:')
print(person)
print(typed_dict_asjson(person, PersonT))
dataclass:
Person(name='John', age=40, musics=[Person.Music(name='Imagine')])
b'{"age":40,"name":"John"}'
TypedDict:
{'name': 'John', 'age': 40, 'musics': [{'name': 'Imagine'}]}
b'{"age":40}'
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.14.1.tar.gz
(53.6 kB
view details)
Built Distribution
File details
Details for the file jsondaora-0.14.1.tar.gz
.
File metadata
- Download URL: jsondaora-0.14.1.tar.gz
- Upload date:
- Size: 53.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43f4a46835798cbed4d952a2e9d05f6ffa49fae7e35fc16728fa29cab5be4deb |
|
MD5 | dd1eccd2102c1cec130a21b0c15a8a88 |
|
BLAKE2b-256 | ce17c811dabded55e93454a46aaa9e2a4a9896b0b224aca357bd578defd33876 |
File details
Details for the file jsondaora-0.14.1-py3-none-any.whl
.
File metadata
- Download URL: jsondaora-0.14.1-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.22.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0a7fe594595e5035fe05a42732c95a80427e3ff577ac144b1c7f6e3a76859fec |
|
MD5 | 7769904dc24f3a5cffe702a58338b8fd |
|
BLAKE2b-256 | fbebadb0cbe15d8c3507d475fae323072154cef235761316d6126abef56b0cdb |