SlySerialize.converters#
Converter and Loader implementations for common types
Classes
|
Collection of many converters to handle many types at once |
|
Converts dataclasses |
Converts datetimes |
|
Converts delayed annotations (string annotations) |
|
Converts dicts with string keys |
|
Converts string or integer enums |
|
Converts classes that have a from_json method |
|
Converts common scalar types |
|
Converts lists and sets |
|
|
Collection of many loaders to handle many types at once |
|
Delays all conversions until complete() is called once. |
Converts classes that have both from_json and to_json methods |
|
Converts classes that have a to_json method |
|
Converts tuples |
|
Converts type variables inside of instances of generic types |
|
Converts unions |
|
|
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
- can_load(cls: type) bool #
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.UnloaderCollection(*unloaders: Unloader[Domain, Any])#
Bases:
Unloader
[Domain
,Any
]Collection of many unloaders to handle many types at once
- can_unload(cls: type) bool #
Whether this converter should be used to serialize the given type
- 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
- 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.
- 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
- 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).
- 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.