nqxpack.registry.register_serialization#
- nqxpack.registry.register_serialization(cls, serialization_fun, deserialization_fun=None, reconstruct_type=True, override=False, min_version=(0, 0, 0))[source]#
Register a custom serialization function and deserialization function for a given class.
- Parameters:
cls (
type[TypeVar(T)]) – The class to register the serialization forserialization_fun (
Callable[[TypeVar(T),tuple[str,...],AssetManager],dict] |Callable[[TypeVar(T)],Any]) – A function that takes an instance of the class and returns a dictionary with the serialized data. If the dictionary has a key “_target_”, it will be used to deserialize the object. If it does not have it, it will be added. If the function does not return a dictionary, no _target_ entry is added.deserialization_fun (
Callable[[dict],TypeVar(T)] |None) – A function that takes the serialized dictionary and returns an instance of the class. If unspecified, the deserialization will use the “_target_” key to resolve the function to call.reconstruct_type (
bool) – If True (default) the output ofserialization_funmust be a dictionary, and the_target_field will be used to reconstruct the original field. If False, instead, the output can be any type handled by the serialisation library, and will not be reserialized. This can be used to serialize some types that you do not actually want to serialise, and convert them to default types.min_version (
tuple[int,int,int]) – Minimum package version (inclusive) that was used to save/serialize data in this format. Default is (0, 0, 0). This indicates “data saved by package version >= min_version uses this format”. When loading, the deserialization function will be selected based on the version that was used to save, not the current package version. The deserialization function will be registered in the versioned registry with this min_version.override (bool)