JSON decoding for Python with type hinting (PEP 484).
JSON decoding for Python with type hinting (PEP 484).
str
, int
, float
, bool
and None
.Union
and Optional
.Tuple
and List
.Tuple
.json.load
and json.loads
.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.
Please read CONTRIBUTING.md.
Set
and Dict
explicitly.json.dump
and json.dumps
.
@typedjson.encodable
decorator.typedjson.decode
.TypedDict
.