databind-json

4.5.4 · deprecated · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

Demonstrates basic serialization and deserialization of dataclasses, including handling of `Optional` fields and nested dataclasses, using the `dumps` and `loads` functions from `databind_json`.

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)

view raw JSON →