|
1 |
| -# databind |
| 1 | +# `databind` |
2 | 2 |
|
3 |
| -__Compatibility__: Python 3.6.3+ |
| 3 | +Databind is a Python serialization library on top of dataclasses, inspired by similar libraries from other languages |
| 4 | +like [jackson-databind](https://github.com/FasterXML/jackson-databind) and [serde-rs](https://serde.rs/). |
4 | 5 |
|
5 |
| -Databind is a library inspired by jackson-databind to de-/serialise Python dataclasses. |
| 6 | + |
| 7 | +[](https://pypi.org/project/pydoc-markdown/) |
6 | 8 |
|
7 |
| -If you install the `databind` package, you will get the respective version of the |
8 |
| -following packages: |
| 9 | +[Examples](https://niklasrosenstein.github.io/python-databind/examples/) | [Changelog](https://niklasrosenstein.github.io/python-databind/changelog/databind.core/) |
9 | 10 |
|
10 |
| -* [databind.core](https://pypi.org/project/databind.core/) – Provides the core framework. |
11 |
| -* [databind.json](https://pypi.org/project/databind.json/) – De-/serialize dataclasses to/from JSON payloads. |
| 11 | +## Overview 📖 |
12 | 12 |
|
13 |
| -## Supported features |
| 13 | +The `databind.core` package provides the core framework for databind. It is then used by `databind.json` to provide |
| 14 | +comprehensive serializatio support between Python and JSON-like data structure. The serialization can easily be |
| 15 | +extended to YAML or TOML by combining it with respective libraries (e.g. `pyaaml` and `tomli`). |
14 | 16 |
|
15 |
| -| Feature | Python version | Databind version | |
16 |
| -| ------- | -------------- | ---------------- | |
17 |
| -| [PEP585](https://www.python.org/dev/peps/pep-0585/) | 3.9 | 1.2.0 – *current* | |
18 |
| -| [PEP585](https://www.python.org/dev/peps/pep-0585/) (forward references) | 3.9 | 1.3.1? – *current* | |
19 |
| -| Resolve type parameters of specialised generic types | 3.x | 1.5.0 – *current* | |
20 |
| -| `typing.TypedDict` | 3.x | 2.0.0 – *current* | |
21 |
| -| Concretise type variables in parametrised generics | 3.x | 2.0.0 – *current* | |
| 17 | +```python |
| 18 | +@dataclass |
| 19 | +class Server: |
| 20 | + host: str |
| 21 | + port: int |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class Config: |
| 25 | + server: Server |
| 26 | + |
| 27 | +from databind.json import dump, load |
| 28 | +assert load({"server": {"host": "localhost", "port": 8080}}, Config) == Config(server=Server(host='localhost', port=8080)) |
| 29 | +assert dump(Config(server=Server(host='localhost', port=8080)), Config) == {"server": {"host": "localhost", "port": 8080}} |
| 30 | +``` |
| 31 | + |
| 32 | +If you install the `databind` proxy package, you get matching versions of `databind.core` and `databind.json`. |
| 33 | + |
| 34 | +## Features ✨ |
| 35 | + |
| 36 | + [typeapi]: https://github.com/NiklasRosenstein/python-typeapi |
| 37 | + |
| 38 | +* Support for a plethora of builtin types, including `Enum`, `datetime`, `date`, `time`, `timedelta`, `uuid`, `pathlib.Path` |
| 39 | +* Support for multiple union serialization modes (nested, flat, keyed, `typing.Literal`) |
| 40 | +* Support for generic types, e.g. `load([{"name": "Jane Doe"}], list[Person])` |
| 41 | +* Support for new-style type hints in older Python versions when using forward refererences (strings or `__future__.annotations`) thanks to [typeapi][] |
| 42 | + * [PEP 604 - Allow writing union types as X | Y](https://www.python.org/dev/peps/pep-0604/) |
| 43 | + * [PEP585 - Type Hinting Generics in Standard Collections](https://www.python.org/dev/peps/pep-0585/)) |
| 44 | +* Full runtime type checking during serialization |
22 | 45 |
|
23 | 46 | ---
|
24 | 47 |
|
|
0 commit comments