fast converter your json to dataclass
Project description
Install
pip install dataclasses_ujson
JSON with DataClasses
The library provides a simple API for decoding JSON to dataclasses.
You can use nested dataclasses. The library uses ujson
for better performance.
There is support for validate simply type: str, int, float, bool, Optional
Minimal python version: python 3.7
Examples
from typing import List, Dict
from dataclasses import dataclass
from dataclasses_ujson.dataclasses_ujson import UJsonMixin
json_string = """
{"a": 1, "b": [{"x": 1}, {"x": 2}], "c": {"x": 1}}
"""
@dataclass(frozen=True)
class JsonDict(UJsonMixin):
x: Dict[str, int]
@dataclass(frozen=True)
class JsonClass(UJsonMixin):
a: int
b: List[JsonDict]
c: Dict[str, int]
data = JsonClass.loads(json_string)
print(data.c["x"])
print(list(data.b)[0].x)
All lists will be returned as generators
from typing import List, Dict
from dataclasses import dataclass
from dataclasses_ujson.dataclasses_ujson import UJsonMixin
json_string = """
[{"x": 1}, {"x": 2}]
"""
@dataclass(frozen=True)
class JsonDict(UJsonMixin):
x: Dict[str, int]
data = JsonDict.loads(json_string, many=True)
print(data) # generator object UJsonMixin
print(list(data)) # list of JsonDict
Performance:
Libraries were compared:
- The default library
json
of python - The library
ujson
https://github.com/esnme/ultrajson - The library
dataclasses-json
https://github.com/lidatong/dataclasses-json
The script is placed in repository (bench_marks.py):
Name of library | Results |
---|---|
json | 0.62s |
ujson | 0.53s |
dataclasses-ujson | 1.24s (python3.10) |
dataclasses-json | 21.6s |
if generator will be returned (using flag many=true)
Name of library | Results |
---|---|
json | 0.62s |
ujson | 0.54s |
dataclasses-ujson | 0.59s |
dataclasses-json | - |
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
Built Distribution
File details
Details for the file dataclasses_ujson-0.0.10.tar.gz
.
File metadata
- Download URL: dataclasses_ujson-0.0.10.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d2bd73d1bda16d13c13405ca90a0b8cebe370ff17cf2b4bdf5c2d4a4676d848 |
|
MD5 | 6ca70999b3446e5b12e1ec651dfa01d3 |
|
BLAKE2b-256 | 0b70b15c526989b8a74feeee4a97974fd1807861332758e8147532f4f9e40d5e |
File details
Details for the file dataclasses_ujson-0.0.10-py3-none-any.whl
.
File metadata
- Download URL: dataclasses_ujson-0.0.10-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.6.0 importlib_metadata/4.8.2 pkginfo/1.7.1 requests/2.23.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7e8ef465bf4ab7b0b46bdad8a9aa80f34eac6819f40bca019ee6afccab8aa07c |
|
MD5 | 557553a740b152af6c8a6d558ba8ac29 |
|
BLAKE2b-256 | befd82bf5c7fb24f45644806d8fe77901d1d8432107cecd2ba5cb13e8941d75a |