Databind Core
Databind Core is a Python library, currently at version 4.5.4, designed for de-serializing Python dataclasses, drawing inspiration from Java's Jackson Databind. It is compatible with Python 3.8 and newer. This package is now deprecated; users are advised to migrate to the `databind` package, which consolidates `databind-core` and `databind-json`.
Common errors
-
ModuleNotFoundError: No module named 'databind.json'
cause Attempting to import `databind.json` after `databind-core`'s JSON functionality was moved into the new, consolidated `databind` package.fixInstall the `databind` package (`pip install databind`) and ensure your import statement is `from databind.json import load, dump`. -
RecursionError: maximum recursion depth exceeded while calling a Python object
cause This error often occurs in older `databind-core` versions (prior to 4.5.4) due to a bug where `Union` settings inherited through the Method Resolution Order (MRO) would cause an infinite recursion loop during de/serialization.fixUpgrade `databind-core` to version 4.5.4 or higher, or preferably, migrate to the `databind` package. -
TypeError: Cannot deserialize into a generic type without type arguments.
cause When deserializing into a dataclass that inherits from an uninstantiated `Generic` type, older `databind-core` versions (prior to 4.5.3) could fail to correctly resolve and serialize all fields.fixUpgrade `databind-core` to version 4.5.3 or higher, or migrate to the `databind` package.
Warnings
- deprecated The `databind-core` package is officially deprecated. All new development and maintenance will focus on the unified `databind` package. Users should migrate to `databind` for continued support and new features.
- breaking Version 4.5.0 of `databind-core` dropped support for Python 3.6 and 3.7. Attempting to use this version on unsupported Python versions will lead to installation or runtime errors.
- gotcha Prior to version 4.5.4, `Union` settings inherited via MRO could cause `RecursionError` during serialization/deserialization, especially with complex inheritance hierarchies involving `Union` types.
- gotcha Prior to version 4.5.3, dataclasses inheriting from uninstantiated `Generic` types might not have all their fields correctly serialized, leading to incomplete output.
- gotcha Before version 4.5.3, `Union` types containing `typing.Literal` were unable to correctly de/serialize, resulting in errors when processing data that matched these patterns.
Install
-
pip install databind-core -
pip install databind
Imports
- load, dump
from databind.core.json import load, dump
from databind.json import load, dump
- dataclass
from dataclasses import dataclass
Quickstart
from dataclasses import dataclass
from databind.json import dump, load
import os
# Note: This quickstart uses the 'databind' package (the recommended successor)
# as 'databind-core' is deprecated and its JSON functionality has moved.
@dataclass
class Server:
host: str
port: int
@dataclass
class Config:
server: Server
# Example payload
dict_payload = {"server": {"host": "localhost", "port": 8080}}
# Load data into a dataclass instance
loaded_config = load(dict_payload, Config)
print(f"Loaded Config: {loaded_config}")
# Dump a dataclass instance back to a dictionary
dumped_payload = dump(loaded_config, Config)
print(f"Dumped Payload: {dumped_payload}")
assert loaded_config == Config(server=Server(host="localhost", port=8080))
assert dumped_payload == dict_payload