Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Interoperates @dataclass with json objects

Project description

jsondaora

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
  • 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}'

Wins Pydantic Benchmark

jsondaora is up to 3.2 times faster than pydantic on it's benchmark

pydantic benchmark

Project details


Download files

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

Files for jsondaora, version 0.9.0
Filename, size File type Python version Upload date Hashes
Filename, size jsondaora-0.9.0-py3-none-any.whl (34.9 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size jsondaora-0.9.0.tar.gz (76.5 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page