asphalt.serialization.api

class asphalt.serialization.api.CustomTypeCodec(*args, **kwds)

Bases: Generic[T_Serializer]

Interface for customizing how custom types are encoded and decoded.

abstract register_object_decoder_hook(serializer)

Register a callback on the serializer for unmarshalling previously marshalled objects.

Parameters:

serializer (TypeVar(T_Serializer, bound= CustomizableSerializer)) – the serializer instance to use

Return type:

None

abstract register_object_encoder_hook(serializer)

Register a custom encoder callback on the serializer.

This callback would be called when the serializer encounters an object it cannot natively serialize. What the callback returns is specific to each serializer type.

Parameters:

serializer (TypeVar(T_Serializer, bound= CustomizableSerializer)) – the serializer instance to use

Return type:

None

class asphalt.serialization.api.CustomizableSerializer(custom_type_codec)

Bases: Serializer

This abstract class defines an interface for registering custom types on a serializer so that the serializer can be extended to (de)serialize a broader array of classes.

Variables:
  • marshallers (Dict[str, Callable]) – a mapping of class -> (typename, marshaller callback)

  • unmarshallers (Dict[str, Callable]) – a mapping of class -> (typename, unmarshaller callback)

register_custom_type(cls, marshaller=<function default_marshaller>, unmarshaller=<function default_unmarshaller>, *, typename=None, wrap_state=True)

Register a marshaller and/or unmarshaller for the given class.

The state object returned by the marshaller and passed to the unmarshaller can be any serializable type. Usually a dictionary mapping of attribute names to values is used.

Warning

Registering marshallers/unmarshallers for any custom type will override any serializer specific encoding/decoding hooks (respectively) already in place!

Parameters:
  • cls (type) – the class to register

  • marshaller (MarshallCallback | None) – a callable that takes the object to be marshalled as the argument and returns a state object

  • unmarshaller (UnmarshallCallback | None) –

    a callable that either:

    • takes an uninitialized instance of cls and its state object as arguments and restores the state of the object

    • takes a state object and returns a new instance of cls

  • typename (str | None) – a unique identifier for the type (defaults to the module:varname reference to the class)

  • wrap_state (bool) – True to wrap the marshalled state before serialization so that it can be recognized later for unmarshalling, False to serialize it as is

Return type:

None

class asphalt.serialization.api.Serializer

Bases: object

This abstract class defines the serializer API.

Each serializer is required to support the serialization of the following Python types, at minimum:

A subclass may support a wider range of types, along with hooks to provide serialization support for custom types.

abstract deserialize(payload)

Deserialize bytes into a Python object.

Return type:

Any

abstract property mimetype: str

Return the MIME type for this serialization format.

abstract serialize(obj)

Serialize a Python object into bytes.

Return type:

bytes