Databind is a library inspired by jackson-databind to de-/serialize Python dataclasses. The `databind` package will install the full suite of databind packages. Compatible with Python 3.7 and newer.
Project description
databind
Databind is a Python serialization library on top of dataclasses, inspired by similar libraries from other languages like jackson-databind and serde-rs.
Overview 📖
The databind.core
package provides the core framework for databind. It is then used by databind.json
to provide
comprehensive serializatio support between Python and JSON-like data structure. The serialization can easily be
extended to YAML or TOML by combining it with respective libraries (e.g. pyaaml
and tomli
).
@dataclass
class Server:
host: str
port: int
@dataclass
class Config:
server: Server
from databind.json import dump, load
assert load({"server": {"host": "localhost", "port": 8080}}, Config) == Config(server=Server(host='localhost', port=8080))
assert dump(Config(server=Server(host='localhost', port=8080)), Config) == {"server": {"host": "localhost", "port": 8080}}
If you install the databind
proxy package, you get matching versions of databind.core
and databind.json
.
Features ✨
- 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
Copyright © 2022 – Niklas Rosenstein
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.