SlySerialize.converters#

Converter and Loader implementations for common types

Classes

CollectionsAbcLoader()

ConverterCollection(*converters[, loaders, ...])

Collection of many converters to handle many types at once

DataclassConverter(allow_extra_keys)

Converts dataclasses

DatetimeConverter()

Converts datetimes

DelayedAnnotationLoader()

Converts delayed annotations (string annotations)

DictConverter()

Converts dicts with string keys

EnumConverter()

Converts string or integer enums

FromJsonLoader()

Converts classes that have a from_json method

JsonScalarConverter()

Converts common scalar types

ListOrSetConverter()

Converts lists and sets

LoaderCollection(*loaders)

Collection of many loaders to handle many types at once

PleaseWaitConverters(*converters)

Delays all conversions until complete() is called once.

ToFromJsonConverter()

Converts classes that have both from_json and to_json methods

ToJsonUnloader()

Converts classes that have a to_json method

TupleConverter()

Converts tuples

TypeVarLoader()

Converts type variables inside of instances of generic types

UnionLoader()

Converts unions

UnloaderCollection(*unloaders)

Collection of many unloaders to handle many types at once

class SlySerialize.converters.JsonScalarConverter#

Bases: Converter[JsonType, int | float | bool | str | None]

Converts common scalar types

can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[JsonScalar]) JsonScalar#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: JsonScalar) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.FromJsonLoader#

Bases: Converter[Any, JsonType]

Converts classes that have a from_json method

can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[Any]) Any#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

abstractmethod can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

abstractmethod ser(ctx: UnloadingContext[Domain, Any], value: T) Domain#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.ToJsonUnloader#

Bases: Unloader[JsonType, Any]

Converts classes that have a to_json method

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

ser(ctx: UnloadingContext[JsonType, Any], value: Any) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.ToFromJsonConverter#

Bases: ToJsonUnloader, FromJsonLoader, Converter[JsonType, JsonType]

Converts classes that have both from_json and to_json methods

can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[Any]) Any#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: Any) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.DataclassConverter(allow_extra_keys: bool)#

Bases: Converter[JsonType, Any]

Converts dataclasses

allow_extra: bool#
can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[Any]) Any#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: Any) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.DictConverter#

Bases: Converter[JsonType, dict[str, T]]

Converts dicts with string keys

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

can_unload(cls: type)#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[dict[str, T]]) dict[str, T]#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: dict[str, T]) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.ListOrSetConverter#

Bases: Converter[JsonType, list[T] | set[T]]

Converts lists and sets

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

can_unload(cls: type)#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[list[T] | set[T]]) list[T] | set[T]#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: list[T] | set[T]) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.CollectionsAbcLoader#

Bases: Loader[JsonType, Sequence[T] | Mapping[str, T]]

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type) Sequence[T] | Mapping[str, T]#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

class SlySerialize.converters.TupleConverter#

Bases: Converter[JsonType, tuple[Any, …]]

Converts tuples

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

can_unload(cls: type)#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[tuple[Any, ...]]) tuple[Any, ...]#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: tuple[Any, ...]) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.TypeVarLoader#

Bases: Loader[Domain, T]

Converts type variables inside of instances of generic types

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

class SlySerialize.converters.UnionLoader#

Bases: Loader[Domain, T]

Converts unions

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

class SlySerialize.converters.DatetimeConverter#

Bases: Converter[JsonType, datetime]

Converts datetimes

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

can_unload(cls: type)#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[datetime.datetime]) datetime#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: datetime) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.EnumConverter#

Bases: Converter[JsonType, EnumType]

Converts string or integer enums

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

can_unload(cls: type)#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[JsonType, Any], value: JsonType, cls: type[EnumType]) EnumType#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

ser(ctx: UnloadingContext[JsonType, Any], value: EnumType) JsonType#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.DelayedAnnotationLoader#

Bases: Loader[Domain, T]

Converts delayed annotations (string annotations)

can_load(cls: type)#

Whether this converter should be used to deserialize the given type

des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

class SlySerialize.converters.LoaderCollection(*loaders: Loader[Domain, Any])#

Bases: Loader[Domain, Any]

Collection of many loaders to handle many types at once

loaders: list[Loader[Domain, Any]]#
with_(*loaders: Loader[Domain, Any])#
can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

find_loader(cls: type) Loader[Domain, Any] | None#
des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

class SlySerialize.converters.UnloaderCollection(*unloaders: Unloader[Domain, Any])#

Bases: Unloader[Domain, Any]

Collection of many unloaders to handle many types at once

unloaders: list[Unloader[Domain, Any]]#
with_(*unloaders: Unloader[Domain, Any])#
can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

find_unloader(cls: type) Unloader[Domain, Any] | None#
ser(ctx: UnloadingContext[Domain, Any], value: Any) Domain#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.ConverterCollection(*converters: Converter[Domain, Any], loaders: list[Loader[Domain, Any]] | None = None, unloaders: list[Unloader[Domain, Any]] | None = None)#

Bases: UnloaderCollection[Domain], LoaderCollection[Domain], Converter[Domain, Any]

Collection of many converters to handle many types at once

unloaders: list[Unloader[Domain, Any]]#
loaders: list[Loader[Domain, Any]]#
with_(*converters: Unloader[Domain, Any] | Loader[Domain, Any])#
can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.

find_loader(cls: type) Loader[Domain, Any] | None#
find_unloader(cls: type) Unloader[Domain, Any] | None#
ser(ctx: UnloadingContext[Domain, Any], value: Any) Domain#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

class SlySerialize.converters.PleaseWaitConverters(*converters: Converter[Domain, Any])#

Bases: ConverterCollection[Domain]

Delays all conversions until complete() is called once. Useful when some converters depend on some variable, long initialization process.

can_load(cls: type) bool#

Whether this converter should be used to deserialize the given type

can_unload(cls: type) bool#

Whether this converter should be used to serialize the given type

find_loader(cls: type) Loader[Domain, Any] | None#
find_unloader(cls: type) Unloader[Domain, Any] | None#
ser(ctx: UnloadingContext[Domain, Any], value: Any) Domain#

Convert a value to a domain-compatible type.

Called only if can_unload returned True for type(value).

with_(*converters: Unloader[Domain, Any] | Loader[Domain, Any])#
unloaders: list[Unloader[Domain, Any]]#
loaders: list[Loader[Domain, Any]]#
wait_flag: Event#
complete()#
await des(ctx: LoadingContext[Domain, Any], value: Domain, cls: type[T]) T#

Convert a domain value to the specified type.

Called only if can_load returned True for cls.