Skip to content

Repository files navigation

JSON Dataclasses

GitHubPyPIGitHub Workflow StatusGitHub Workflow Status

Typed JSON dataclasses deserialisation for Python 3.9+

Check out the docs

Installation

pip install jsondataclasses

Usage

fromdatetimeimportdatefromtypingimportLiteral, Optionalfromjsondataclassesimportjsondataclass, jsonfielddefparse_date(date_string: str) ->date:
returndate.fromisoformat(date_string.replace(".", "-"))
@jsondataclassclassCar:
make: Literal["Ford", "Renault", "Volkswagen"] =jsonfield("carMake")
model: str=jsonfield("model")
manufactured_at: date=jsonfield("dateOfManufacture", parse_date)
num_of_wheels: Optional[int] =jsonfield("numberOfWheels", default_value=4)
car=Car({
"carMake": "Ford",
"model": "Focus",
"dateOfManufacture": "2018.03.14"
})
print(car) # Car(make='Ford', model='Focus', manufactured_at=datetime.date(2018, 3, 14), num_of_wheels=4)

Class field types can be any primitive type (eg. str, int, datetime), a variadic generic (eg. list[str], Optional[int], Literal["hello", "world"]), or even another jsondataclass. The default_value argument of jsonfield will be used if the specified key is not found in the dictionary and the type of the class field is not Optional[...]. In this case, the value of default_value will be passed to the supplied parser function.

About

Typed JSON dataclasses for Python 3.9+

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages