Skip to content

duron.codec module

Codec

Bases: Protocol

Protocol for encoding/decoding Python objects to/from JSON-serializable values.

Implement this protocol to provide custom serialization for types not supported by the default codec (e.g., dataclasses, Pydantic models, custom objects).

decode_json

decode_json(encoded, expected_type)

Reconstruct a Python object from a JSON value during log replay.

Parameters:

  • encoded (JSONValue) –

    The JSON value to decode

  • expected_type (TypeHint[Any]) –

    Type hint for the expected return type

Returns:

  • object

    The reconstructed Python object

encode_json

encode_json(result)

Convert a Python object to a JSON-serializable value for persistence.

Parameters:

  • result (object) –

    The object to encode (e.g., operation result or argument)

Returns:

  • JSONValue

    A JSON-serializable value (None, bool, int, float, str, list, or dict)

Raises:

  • TypeError

    If the object cannot be serialized