Skip to main content

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 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


Download files

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

Source Distribution

dataclasses_ujson-0.0.12.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

dataclasses_ujson-0.0.12-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file dataclasses_ujson-0.0.12.tar.gz.

File metadata

  • Download URL: dataclasses_ujson-0.0.12.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

Hashes for dataclasses_ujson-0.0.12.tar.gz
Algorithm Hash digest
SHA256 af4054d168767f748a826a28b558eeee24ef4ca7305266b88a2e5408ad341a4c
MD5 0bea95144a6cec5fd2a401c98394118d
BLAKE2b-256 7be6332014bc4c1be09c5cf24d2029ba1d78fd3b5a1eb336635e19a0638e1c2e

See more details on using hashes here.

File details

Details for the file dataclasses_ujson-0.0.12-py3-none-any.whl.

File metadata

  • Download URL: dataclasses_ujson-0.0.12-py3-none-any.whl
  • Upload date:
  • Size: 7.7 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

Hashes for dataclasses_ujson-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 4ff7e78ed629a13a5b1205016cc20a996a92baaab540102514a3d59fd5725222
MD5 9b8355f1e75443f85224e3c817f2710f
BLAKE2b-256 9c06fa7a0780c47631190655c00b72d10a1d349273df87ac87f2db3fe22f37bf

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page