Skip to content

serializer

This module contains the Serializer that is used to parse incoming network data into Python classes.

Serializer

Deserializes raw bytes into wom.py model classes.

Source code in wom/serializer.py
class Serializer:
    """Deserializes raw bytes into wom.py model classes."""

    __slots__ = ("_decoders",)

    def __init__(self) -> None:
        self._decoders: DecodersT = {}

    def decode(self, data: bytes, model_type: t.Type[T]) -> T:
        """Decodes the data into the given model type.

        Args:
            data: The JSON payload as bytes.

            model_type: The type of model to decode into.

        Returns:
            The requested model.
        """
        return self.get_decoder(model_type).decode(data)

    def get_decoder(self, model_type: t.Type[T]) -> Decoder[T]:
        """Lazily initializes decoders as they are requested and caches them.

        Args:
            model_type: The model type this decoder will target.

        Returns:
            The requested decoder.
        """
        if not (decoder := self._decoders.get(model_type)):
            decoder = self._decoders[model_type] = Decoder(model_type)

        return decoder  # type: ignore[return-value]

decode

decode(data: bytes, model_type: t.Type[T]) -> T

Decodes the data into the given model type.

Parameters:

Name Type Description Default
data bytes

The JSON payload as bytes.

required
model_type Type[T]

The type of model to decode into.

required

Returns:

Type Description
T

The requested model.

Source code in wom/serializer.py
def decode(self, data: bytes, model_type: t.Type[T]) -> T:
    """Decodes the data into the given model type.

    Args:
        data: The JSON payload as bytes.

        model_type: The type of model to decode into.

    Returns:
        The requested model.
    """
    return self.get_decoder(model_type).decode(data)

get_decoder

get_decoder(model_type: t.Type[T]) -> Decoder[T]

Lazily initializes decoders as they are requested and caches them.

Parameters:

Name Type Description Default
model_type Type[T]

The model type this decoder will target.

required

Returns:

Type Description
Decoder[T]

The requested decoder.

Source code in wom/serializer.py
def get_decoder(self, model_type: t.Type[T]) -> Decoder[T]:
    """Lazily initializes decoders as they are requested and caches them.

    Args:
        model_type: The model type this decoder will target.

    Returns:
        The requested decoder.
    """
    if not (decoder := self._decoders.get(model_type)):
        decoder = self._decoders[model_type] = Decoder(model_type)

    return decoder  # type: ignore[return-value]