Skip to content

serializer

The internal component that encodes requests and decodes responses into models. Owned by the Client; you should not need to use it directly.

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

Serializer

Serializes wom.py model classes to and from raw bytes.

Source code in wom/serializer.py
class Serializer:
    """Serializes wom.py model classes to and from raw bytes."""

    __slots__ = ("_decoders", "_encoder")

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

    def encode(self, obj: t.Any) -> bytes:
        """Encodes the object into JSON bytes.

        Parameters
        ----------
        obj : Any
            The object to encode.

        Returns
        -------
        bytes
            The encoded JSON payload.
        """
        return self._encoder.encode(obj)

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

        Parameters
        ----------
        data : bytes
            The JSON payload as bytes.
        model_type : Type[T]
            The type of model to decode into.

        Returns
        -------
        T
            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.

        Parameters
        ----------
        model_type : Type[T]
            The model type this decoder will target.

        Returns
        -------
        Decoder[T]
            The requested decoder.
        """
        if not (decoder := self._decoders.get(model_type)):
            decoder = self._decoders[model_type] = Decoder(  # pyright: ignore[reportArgumentType]
                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.

    Parameters
    ----------
    data : bytes
        The JSON payload as bytes.
    model_type : Type[T]
        The type of model to decode into.

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

encode

encode(obj: t.Any) -> bytes

Encodes the object into JSON bytes.

Parameters:

Name Type Description Default
obj Any

The object to encode.

required

Returns:

Type Description
bytes

The encoded JSON payload.

Source code in wom/serializer.py
def encode(self, obj: t.Any) -> bytes:
    """Encodes the object into JSON bytes.

    Parameters
    ----------
    obj : Any
        The object to encode.

    Returns
    -------
    bytes
        The encoded JSON payload.
    """
    return self._encoder.encode(obj)

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.

    Parameters
    ----------
    model_type : Type[T]
        The model type this decoder will target.

    Returns
    -------
    Decoder[T]
        The requested decoder.
    """
    if not (decoder := self._decoders.get(model_type)):
        decoder = self._decoders[model_type] = Decoder(  # pyright: ignore[reportArgumentType]
            model_type
        )

    return decoder  # type: ignore[return-value]