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
- 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
- Support type hints with forward reference.
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.7.5.tar.gz
(5.4 kB
view hashes)
Built Distribution
Close
Hashes for typedjson-0.7.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3d46668eb29af6fb259a974817f6024135a249ae3da92eb8d8d1e1cba17e4798 |
|
MD5 | 04d2863f882d47f972020b7474895e9d |
|
BLAKE2b-256 | c34c30b63a61089ca91727f5c0be70488954988a20048c89f2a84455f4e798b9 |