Skip to content

PydanticSerializer

Bases: Serializer[PydanticModel]

Serializes a single pydantic model type to/from its JSON representation. Requires pydantic to be installed (pip install alchemical_queues[pydantic]).

Parameters:

Name Type Description Default
model Type[PydanticModel]

the pydantic model class every entry put through the queue is an instance of.

required
Source code in src/alchemical_queues/serializers.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class PydanticSerializer(Serializer[PydanticModel]):
    """Serializes a single [pydantic](https://docs.pydantic.dev/) model type
    to/from its JSON representation. Requires `pydantic` to be installed
    (`pip install alchemical_queues[pydantic]`).

    Args:
        model (Type[PydanticModel]): the pydantic model class every entry
            put through the queue is an instance of.
    """

    def __init__(self, model: Type[PydanticModel]) -> None:
        self._model = model

    def dumps(self, obj: PydanticModel) -> bytes:
        return obj.model_dump_json().encode("utf-8")  # type: ignore[attr-defined]

    def loads(self, data: bytes) -> PydanticModel:
        return self._model.model_validate_json(data)  # type: ignore[attr-defined]