serialization#

starlite.utils.serialization.default_serializer(value: Any, type_encoders: Optional[Dict[Any, Callable[[Any], Any]]] = None) Any#

Transform values non-natively supported by msgspec

Parameters:
  • value – A value to serialize#

  • type_encoders – Mapping of types to callables to transforming types

Returns:

A serialized value

Raises:

TypeError – if value is not supported

starlite.utils.serialization.encode_json(obj: ~typing.Any, default: ~typing.Optional[~typing.Callable[[~typing.Any], ~typing.Any]] = <function default_serializer>) bytes#

Encode a value into JSON.

Parameters:
  • obj – Value to encode

  • default – Optional callable to support non-natively supported types.

Returns:

JSON as bytes

Raises:

SerializationException – If error encoding obj.

starlite.utils.serialization.decode_json(raw: Union[str, bytes]) Any#
starlite.utils.serialization.decode_json(raw: Union[str, bytes], type_: Type[T]) T

Decode a JSON string/bytes into an object.

Parameters:
  • raw – Value to decode

  • type – An optional type to decode the data into

Returns:

An object

Raises:

SerializationException – If error decoding raw.

starlite.utils.serialization.encode_msgpack(obj: ~typing.Any, enc_hook: ~typing.Optional[~typing.Callable[[~typing.Any], ~typing.Any]] = <function default_serializer>) bytes#

Encode a value into MessagePack.

Parameters:
  • obj – Value to encode

  • enc_hook – Optional callable to support non-natively supported types

Returns:

MessagePack as bytes

Raises:

SerializationException – If error encoding obj.

starlite.utils.serialization.decode_msgpack(raw: bytes) Any#
starlite.utils.serialization.decode_msgpack(raw: bytes, type_: Type[T]) T

Decode a MessagePack string/bytes into an object.

Parameters:
  • raw – Value to decode

  • type – An optional type to decode the data into

Returns:

An object

Raises:

SerializationException – If error decoding raw.