Skip to content

Serializable

serializable

Serializable protocol for messages and domain events.

Provides a Serializable protocol that allows types to declare they can be
converted to and from plain dictionaries. This enables generic serialization
infrastructure (JSON, database adapters, event stores) to work with any type
that satisfies the protocol structurally — no base class required.

Serializable

Bases: Protocol

Protocol for serializable objects.

Types satisfying this protocol can be
  • serialised to a dictionary via to_dict().
  • deserialised from a dictionary via from_dict().

The protocol is structural — any class that defines to_dict and
from_dict with matching signatures satisfies it automatically,
without explicit registration or inheritance.

Source code in src/forging_blocks/infrastructure/serialization/serializable.py
class Serializable[T: Mapping[str, object]](Protocol):
    """Protocol for serializable objects.

    Types satisfying this protocol can be:
      - serialised to a dictionary via ``to_dict()``.
      - deserialised from a dictionary via ``from_dict()``.

    The protocol is structural — any class that defines ``to_dict`` and
    ``from_dict`` with matching signatures satisfies it automatically,
    without explicit registration or inheritance.
    """

    def to_dict(self) -> dict[str, object]:
        """Return a dictionary representation of this instance."""
        ...

    @classmethod
    def from_dict(cls, data: dict[str, object]) -> Self:
        """Create an instance from a dictionary representation.

        Args:
            data: Dictionary containing the serialised representation.

        Returns:
            A new instance reconstituted from *data*.
        """
        ...

to_dict() -> dict[str, object]

Return a dictionary representation of this instance.

Source code in src/forging_blocks/infrastructure/serialization/serializable.py
def to_dict(self) -> dict[str, object]:
    """Return a dictionary representation of this instance."""
    ...

from_dict(data: dict[str, object]) -> Self classmethod

Create an instance from a dictionary representation.

Parameters:

Name Type Description Default
data dict[str, object]

Dictionary containing the serialised representation.

required

Returns:

Type Description
Self

A new instance reconstituted from data.

Source code in src/forging_blocks/infrastructure/serialization/serializable.py
@classmethod
def from_dict(cls, data: dict[str, object]) -> Self:
    """Create an instance from a dictionary representation.

    Args:
        data: Dictionary containing the serialised representation.

    Returns:
        A new instance reconstituted from *data*.
    """
    ...