decision_rules.serialization
decision_rules.serialization.utils
Contains classed useful for serializing and deserializing objects.
- class decision_rules.serialization.utils.JSONClassSerializer
Bases:
ABCAbstract class for classes serializer. Each serializer should inherit this class and be registered for type by “register_serializer”. Each class serializer should have inner class “Model” which is pydantic model class for JSON representation.
- classmethod deserialize(data: dict | BaseModel) Any
- Parameters:
data – (Union[dict, BaseModel]): dictionary or pydantic model
- Returns:
object instance
- Return type:
Any
- classmethod serialize(instance: Any, mode: str | SerializationModes = SerializationModes.FULL) dict
- Parameters:
instance (Any) – object instance
mode (Union[str, SerializationModes], optional) – Controls serialization mode. In minimal mode only minimal information is serialized. After deserialization of the rulesets you’ll have to call update method to use it. In full mode all important information is serialized and after ruleset deserialization and you can use it further without calling update. Defaults to SerializationModes.FULL
- Returns:
json dictionary
- Return type:
dict
- class decision_rules.serialization.utils.JSONSerializer
Bases:
objectSerializes deserializes registered object types. It can serialize and deserialize any object for which type there was registered as serializer class.
- classmethod deserialize(data: dict, target_class: type) Any
Deserializes json dictionary to given target class
- Parameters:
data (dict) – json dictionary
target_class (type) – target class
- Raises:
ValueError – if no serializer class is registered for passed object type
- Returns:
deserialized object
- Return type:
Any
- classmethod register_serializer(serializable_class: type, serializer_class: type)
Registere new serializer for given type
- Parameters:
serializable_class (type) – serializer class
serializer_class (type) – type of objects to be serialized / deserialized
- Raises:
ValueError – when try to register multiple serializers for the same type
- classmethod serialize(value: Any, mode: str | SerializationModes = SerializationModes.FULL) dict
Serializes given object to json dictionary
- Parameters:
value (Any) – value
mode (Union[str, SerializationModes], optional) – Controls serialization mode. In minimal mode only minimal information is serialized. After deserialization of the rulesets you’ll have to call update method to use it. In full mode all important information is serialized and after ruleset deserialization and you can use it further without calling update. Defaults to SerializationModes.FULL
- Raises:
ValueError – if no serializer class is registered for passed object type
- Returns:
json dictionary
- Return type:
dict
- class decision_rules.serialization.utils.SerializationModes(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)
Bases:
EnumSpecifies possible serialization modes choice.
- FULL: str = 'full'
In this mode all important information is serialized. After deserialization of the rulesets you can use it as it is without calling update method.
- MINIMAL: str = 'minimal'
In this mode only minimal, necessary information is serialized. After deserialization of the rulesets you’ll have to call update method to use it.
- classmethod instantiate(value: SerializationModes | str) SerializationModes
Creates serialization mode instance from given value
- Parameters:
value (Union[SerializationModes, str]) – either serialization mode or its name
- Raises:
ValueError – If given value is not a valid serialization mode
- Returns:
serialization mode
- Return type:
- decision_rules.serialization.utils.register_serializer(registered_type: type)
Register decorated class to be used as serializer for given type.
Example
- Parameters:
type (type) – type of objects to be serialized