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.9.0.tar.gz
(5.5 kB
view hashes)
Built Distribution
Close
Hashes for typedjson-0.9.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8bae6e35da3b73672f878b9c3ae501ba163e9d968f29f8c48ae9c7f4b279001 |
|
MD5 | a1890ee71b7cecf8999b034baa1aa12d |
|
BLAKE2b-256 | c2ac8441e5b8c3067b1a00aff4a335cecd8e1ad0d6d1072ee982b537214d83ba |