Skip to main content

typedjson

License Pypi CI

JSON decoding for Python with type hinting (PEP 484).

Requirements and Restrictions

  • Python >= 3.6
  • Mypy <= 0.770
  • Use non-generic or parameterized class to decode JSON.
  • Use type hints without forward references.

Features

  • Support decoding types as below:
    • primitive types like str, int, float, bool and None.
    • Union and Optional.
    • homogeneous and heterogeneous Tuple and List.
    • variable-length Tuple.
    • non-generic and parameterized dataclasses.
  • Support API like json.load and json.loads.

Example

from typing import Optional

import typedjson
from dataclasses import dataclass


@dataclass(frozen=True)
class NameJson:
    first: str
    last: Optional[str]


@dataclass(frozen=True)
class CatJson:
    id: str
    age: int
    name: Optional[NameJson]


json = {
    'id': 'test-cat',
    'age': 13,
    'name': {
        'first': 'Jiji',
    },
}

print(typedjson.decode(CatJson, json))  # Output: CatJson(id='test-cat', age=13, name=NameJson(first='Jiji', last=None))

print(typedjson.decode(CatJson, {}))  # Output: DecodingError(TypeMismatch(('id',)))

Please refer to test codes for more detail.

Contributions

Please read CONTRIBUTING.md.

TODO

  • Prohibit decoding Set and Dict explicitly.
  • Provide the API document.
  • Explain why typedjson uses undocumented APIs.
  • Explain what typedjson resolves.
  • Improve API to dump like json.dump and json.dumps.
    • Provide mypy plugin to check whether the class is encodable as JSON or not with @typedjson.encodable decorator.
  • Improve the peformance of typedjson.decode.
  • Support type hints with forward reference.
  • Support TypedDict.

Release files for typedjson 0.10.4

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

Source distribution (sdist)

Source distribution for typedjson 0.10.4
File Size Uploaded
typedjson-0.10.4.tar.gz 5.8 kB Details

Built distribution (wheel)

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

Total release size: 12.4 kB

Release files / typedjson-0.10.4.tar.gz

Download URL typedjson-0.10.4.tar.gz
Size 5.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d2f987b903c857edcde88e0597ac7eeec7660e78c361632cad3cdb81bd2180d4
BLAKE2b-256 checksum
How to use checksums
1c5e25c93501b06fd6b6830e4acb0d226833cfed1d94bc7440d4fc08d09693c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.7 Linux/5.8.0-44-generic

Release files / typedjson-0.10.4-py3-none-any.whl

Download URL typedjson-0.10.4-py3-none-any.whl
Size 6.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f55232634a69c5d4a0b699dfde81834ab72a416d898b4a136288a72494712e35
BLAKE2b-256 checksum
How to use checksums
0e5a7722bc9a0dde3419cb08297607e2487143fa8801d50586d077ad03779c65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.7 Linux/5.8.0-44-generic

Release history Release notifications | RSS feed

This release

0.10.4 This release

2 release files

0.10.0

1 release file

0.9.0

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.1

1 release file

0.7.0

1 release file

0.6.0

1 release file

0.5.0

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

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