Module aws_lambda_powertools.utilities.idempotency.serialization.dataclass

Classes

class DataclassSerializer (model: type[DataClass])

A serializer class for transforming data between dataclass objects and dictionaries.

Parameters

model : type[DataClass]
A dataclass type to be used for serialization and deserialization
Expand source code
class DataclassSerializer(BaseIdempotencyModelSerializer):
    """
    A serializer class for transforming data between dataclass objects and dictionaries.
    """

    def __init__(self, model: type[DataClass]):
        """
        Parameters
        ----------
        model: type[DataClass]
            A dataclass type to be used for serialization and deserialization
        """
        self.__model: type[DataClass] = model

    def to_dict(self, data: DataClass) -> dict:
        return asdict(data)

    def from_dict(self, data: dict) -> DataClass:
        return self.__model(**data)

    @classmethod
    def instantiate(cls, model_type: Any) -> BaseIdempotencySerializer:
        if model_type is None:
            raise IdempotencyNoSerializationModelError("No serialization model was supplied")

        if not is_dataclass(model_type):
            raise IdempotencyModelTypeError("Model type is not inherited of dataclass type")
        return cls(model=model_type)  # type: ignore[arg-type]

Ancestors

Methods

def from_dict(self, data: dict) ‑> Any
def to_dict(self, data: DataClass) ‑> dict

Inherited members