databind-json
The `databind-json` library (version 4.5.4) provided de-/serialization capabilities for Python dataclasses to and from JSON payloads, compatible with Python 3.8 and newer. This package is officially deprecated. Its core functionality has been integrated directly into the `databind` library, specifically within the `databind.json` module. While `databind-json` has received recent maintenance updates, new projects and existing users are strongly advised to migrate to the `databind` package for all serialization needs, benefiting from broader support and continued development.
Common errors
-
RecursionError: maximum recursion depth exceeded while calling a Python object
cause This error can occur in older versions when `Union` settings are inherited via the Method Resolution Order (MRO) in complex dataclass hierarchies, leading to infinite recursion during de/serialization.fixUpgrade `databind-json` to version `4.5.4` or newer. If you have migrated, ensure your `databind` package is at `4.5.4` or newer. -
TypeError: object of type Literal cannot be used as a type argument
cause Versions prior to `4.5.3` had issues correctly processing `typing.Union` types that included `typing.Literal` as one of the possible types during de/serialization.fixUpgrade `databind-json` to version `4.5.3` or newer. If you have migrated, ensure your `databind` package is at `4.5.3` or newer. -
Dataclass fields missing from serialized output when inheriting from Generic
cause Dataclasses inheriting from uninstantiated `typing.Generic` classes in versions prior to `4.5.3` did not have all their fields properly discovered and serialized by the library.fixUpgrade `databind-json` to version `4.5.3` or newer. If you have migrated, ensure your `databind` package is at `4.5.3` or newer.
Warnings
- breaking The `databind-json` package is officially deprecated and is no longer the recommended library for JSON serialization of dataclasses. Its functionality has been fully integrated into the `databind` library under the `databind.json` module.
- gotcha Incorrect handling or complex definitions of `typing.Union` or `typing.Optional` types within dataclass fields can lead to unexpected serialization failures or `RecursionError`. This is especially relevant when `Union` types include `typing.Literal` values or when Union settings are inherited via MRO.
- gotcha Dataclasses inheriting from uninstantiated `typing.Generic` types may not have all their fields correctly discovered and serialized. Similarly, complex inheritance structures involving `__databind_settings__` on subclasses can introduce subtle bugs or unexpected behavior.
Install
-
pip install databind-json -
pip install databind
Imports
- dumps
from databind_json import dumps
- loads
from databind_json import loads
Quickstart
from dataclasses import dataclass
from typing import Optional, Union
from databind_json import dumps, loads
@dataclass
class Address:
street: str
city: str
@dataclass
class User:
id: int
name: str
email: Optional[str] = None
address: Optional[Address] = None
# Serialize a User object
user = User(id=123, name="Alice Smith", email="alice@example.com", address=Address(street="123 Main St", city="Anytown"))
json_payload = dumps(user)
print("Serialized JSON:", json_payload)
# Deserialize a JSON string back into a User object
json_string_to_load = '{"id": 456, "name": "Bob Johnson", "email": null, "address": {"street": "456 Oak Ave", "city": "Otherville"}}'
deserialized_user = loads(json_string_to_load, User)
print("Deserialized User:", deserialized_user)
# Example with an optional field not present in JSON (deserializes to None)
json_no_address = '{"id": 789, "name": "Charlie Brown"}'
user_no_address = loads(json_no_address, User)
print("User with no address in JSON:", user_no_address)