serialization#

litestar.serialization.default_deserializer(target_type: Any, value: Any, type_decoders: TypeDecodersSequence | None = None) Any#

Transform values non-natively supported by msgspec

Parameters:
  • target_type – Encountered type

  • value – Value to coerce

  • type_decoders – Optional sequence of type decoders

Returns:

A msgspec-supported type

litestar.serialization.decode_json(value: str | bytes, target_type: type[T] | EmptyType = _EmptyEnum.EMPTY, type_decoders: TypeDecodersSequence | None = None, strict: bool = True) Any#

Decode a JSON string/bytes into an object.

Parameters:
  • value – Value to decode

  • target_type – An optional type to decode the data into

  • type_decoders – Optional sequence of type decoders

  • strict – Whether type coercion rules should be strict. Setting to False enables a wider set of coercion rules from string to non-string types for all values

Returns:

An object

Raises:

SerializationException – If error decoding value.

litestar.serialization.decode_msgpack(value: bytes, target_type: type[T] | EmptyType = _EmptyEnum.EMPTY, type_decoders: TypeDecodersSequence | None = None, strict: bool = True) Any#

Decode a MessagePack string/bytes into an object.

Parameters:
  • value – Value to decode

  • target_type – An optional type to decode the data into

  • type_decoders – Optional sequence of type decoders

  • strict – Whether type coercion rules should be strict. Setting to False enables a wider set of coercion rules from string to non-string types for all values

Returns:

An object

Raises:

SerializationException – If error decoding value.

litestar.serialization.default_serializer(value: Any, type_encoders: Mapping[Any, Callable[[Any], Any]] | None = None) Any#

Transform values non-natively supported by msgspec

Parameters:
  • value – A value to serialized

  • type_encoders – Mapping of types to callables to transforming types

Returns:

A serialized value

Raises:

TypeError – if value is not supported

litestar.serialization.encode_json(value: Any, serializer: Callable[[Any], Any] | None = None) bytes#

Encode a value into JSON.

Parameters:
  • value – Value to encode

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

Returns:

JSON as bytes

Raises:

SerializationException – If error encoding obj.

litestar.serialization.encode_msgpack(value: ~typing.Any, serializer: ~typing.Callable[[~typing.Any], ~typing.Any] | None = <function default_serializer>) bytes#

Encode a value into MessagePack.

Parameters:
  • value – Value to encode

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

Returns:

MessagePack as bytes

Raises:

SerializationException – If error encoding obj.

litestar.serialization.get_serializer(type_encoders: TypeEncodersMap | None = None) Serializer#

Get the serializer for the given type encoders.