JSON decoding for Python with type hinting (PEP 484)
Project description
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
,bool
andNone
. Union
andOptional
.- homogeneous and heterogeneous
Tuple
andList
. - variable-length
Tuple
. - non-generic and parameterized dataclasses.
- primitive types like
- Support API like
json.load
andjson.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
andDict
explicitly. - Provide the API document.
- Explain why typedjson uses undocumented APIs.
- Explain what typedjson resolves.
- Improve API to dump like
json.dump
andjson.dumps
.- Provide mypy plugin to check whether the class is encodable as JSON or not with
@typedjson.encodable
decorator.
- 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
.
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
typedjson-0.10.4.tar.gz
(5.8 kB
view details)
Built Distribution
File details
Details for the file typedjson-0.10.4.tar.gz
.
File metadata
- Download URL: typedjson-0.10.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/5.8.0-44-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
d2f987b903c857edcde88e0597ac7eeec7660e78c361632cad3cdb81bd2180d4
|
|
MD5 |
cd6dd08fca6d27dc2687379ebd2191aa
|
|
BLAKE2b-256 |
1c5e25c93501b06fd6b6830e4acb0d226833cfed1d94bc7440d4fc08d09693c7
|
File details
Details for the file typedjson-0.10.4-py3-none-any.whl
.
File metadata
- Download URL: typedjson-0.10.4-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.4 CPython/3.8.7 Linux/5.8.0-44-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
f55232634a69c5d4a0b699dfde81834ab72a416d898b4a136288a72494712e35
|
|
MD5 |
8abe8fe8118e140e3c234c2a294ce3cc
|
|
BLAKE2b-256 |
0e5a7722bc9a0dde3419cb08297607e2487143fa8801d50586d077ad03779c65
|