mitsuse / typedjson-python

JSON decoding for Python with type hinting (PEP 484).
MIT License
29 stars 5 forks source link
json python type-hints validation

typedjson

License Pypi CI

JSON decoding for Python with type hinting (PEP 484).

Requirements and Restrictions

Features

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