The databind
package provides a (de)serialization framework that understands most native Python types as well as
dataclasses, as well as an implementation for serialize to/from JSON-like nested data structures.
Databind is intended mostly for flexible and easy to use configuration loading. It does not try achieve high-performance; you should look towards e.g. mashumaro for this usecase.
@dataclass
class Server:
host: str
port: int
@dataclass
class Config:
server: Server
from databind.json import dump, load
dict_payload = {"server": {"host": "localhost", "port": 8080}}
loaded = Config(server=Server(host="localhost", port=8080))
assert load(dict_payload, Config) == loaded
assert dump(loaded, Config) == dict_payload
- Support for a plethora of builtin types, including
Enum
,Decimal
,UUID
,Path
,datetime
,date
,time
,timedelta
- Support for multiple union serialization modes (nested, flat, keyed,
typing.Literal
) - Support for generic types, e.g.
load([{"name": "Jane Doe"}], list[Person])
- Support for new-style type hints in older Python versions when using forward refererences (strings or
__future__.annotations
) thanks to typeapi - Support for customized serialization and deserialization of types
- Support for flattening fields of a nested dataclass or collecting remaining fields in a
dict
- Full runtime type checking during serialization
- Use "settings" to customize serialization behaviour
- As global settings per
load()
/dump()
call:load(..., settings=[ExtraKeys(True)])
- As class-level settings using a decorator:
@Union(style=Union.FLAT)
or@ExtraKeys(True)
- As type-hint level settings using
typing.Annotated
(ortyping_extensions.Annotated
):full_name: Annotated[str, Alias("fullName")]
orFullNameField = Annotated[str, Alias("fullName")]
- As global settings per
- Merged
databind.core
anddatabind.json
packages intodatabind
. The old PyPI packages will remain as proxies until the next minor version. - Dropped support for Python 3.6 and 3.7.
Copyright © 2022 – Niklas Rosenstein