Dict Message Codec¶
_dict_message_codec
¶
Dict-based message codec that serializes messages to dict[str, object].
DictMessageCodec
¶
Bases: MessageCodec[M, dict[str, object]]
Codec that serializes messages to dict[str, object].
Uses the message's own value property for the payload and
message.metadata.value for the metadata section. Reconstruction
goes through the from_payload_fields classmethod that every
concrete Message subclass provides.
Example
class StubCommand[T](Message[T]):
# Inline stub for the example.
pass
class CreateOrder(StubCommand[dict[str, object]]):
def __init__(self, customer_id: str) -> None:
super().__init__()
self.customer_id = customer_id
@property
def _payload(self) -> dict[str, object]:
return {"customer_id": self.customer_id}
@classmethod
def from_payload_fields(
cls, payload: dict[str, object], metadata: MessageMetadata
) -> CreateOrder:
return cls(customer_id=str(payload["customer_id"]))
@property
def value(self) -> dict[str, object]:
return self._payload
codec = DictMessageCodec[CreateOrder]()
original = CreateOrder(customer_id="cust-42")
encoded = codec.encode(original)
decoded = codec.decode(encoded, CreateOrder)
assert decoded.customer_id == original.customer_id
Source code in src/forging_blocks/infrastructure/serialization/_dict_message_codec.py
encode(message: M) -> dict[str, object]
¶
Encode message to a dictionary with metadata and payload keys.
Source code in src/forging_blocks/infrastructure/serialization/_dict_message_codec.py
decode(data: dict[str, object], message_type: type[M]) -> M
¶
Decode data back into a message of message_type.