typedjson
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,boolandNone. UnionandOptional.- homogeneous and heterogeneous
TupleandList. - variable-length
Tuple. - non-generic and parameterized dataclasses.
- primitive types like
- Support API like
json.loadandjson.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
SetandDictexplicitly. - Provide the API document.
- Explain why typedjson uses undocumented APIs.
- Explain what typedjson resolves.
- Improve API to dump like
json.dumpandjson.dumps.- Provide mypy plugin to check whether the class is encodable as JSON or not with
@typedjson.encodabledecorator.
- Provide mypy plugin to check whether the class is encodable as JSON or not with
- 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)
| File | Size | Uploaded | |
|---|---|---|---|
| typedjson-0.10.4.tar.gz | 5.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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
|