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.4.tar.gz
(5.3 kB
view hashes)
Built Distribution
Close
Hashes for typedjson-0.7.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1dcc5a90fa0ed096f9e54b8a91a3b587f6728d3697f55c2498eb28e46851488a |
|
MD5 | 7f3242e3c746a84f46729b1c0b9d5bb0 |
|
BLAKE2b-256 | 91638541f4d58fccb1981f020056146911493b80b862d7127293df336bb6accf |