Skip to main content

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

Metadata

Release files for jsondaora 0.14.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsondaora 0.14.1
File Size Uploaded
jsondaora-0.14.1.tar.gz 53.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for jsondaora 0.14.1
File Interpreter ABI Platform
jsondaora-0.14.1-py3-none-any.whl Python 3 none any Details

Total release size: 69.9 kB

Release files / jsondaora-0.14.1.tar.gz

Download URL jsondaora-0.14.1.tar.gz
Size 53.6 kB
Tags Source
SHA-256 checksum
How to use checksums
43f4a46835798cbed4d952a2e9d05f6ffa49fae7e35fc16728fa29cab5be4deb
BLAKE2b-256 checksum
How to use checksums
ce17c811dabded55e93454a46aaa9e2a4a9896b0b224aca357bd578defd33876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release files / jsondaora-0.14.1-py3-none-any.whl

Download URL jsondaora-0.14.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a7fe594595e5035fe05a42732c95a80427e3ff577ac144b1c7f6e3a76859fec
BLAKE2b-256 checksum
How to use checksums
fbebadb0cbe15d8c3507d475fae323072154cef235761316d6126abef56b0cdb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.22.0

Release history Release notifications | RSS feed

This release

0.14.1 This release

2 release files

0.14.0

2 release files

0.13.4

2 release files

0.13.3

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.2

2 release files

0.3.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page