Skip to main content
Version: latest

Serialization

The serialization subpackage groups all serialization components and serializer implementations.

Base classes are defined which can allow for other serialization implementations beside the built-in specification serializers.

class MsgSpecSerializer

Bases: Serializer

MsgSpecSerializer(encoding, bool timestamps_as_str=False, bool timestamps_as_iso8601=False)

Provides a serializer for either the ‘MessagePack’ or ‘JSON’ specifications.

  • Parameters:
    • encoding (Callable) – The msgspec encoding type.
    • timestamps_as_str (bool , default False) – If the serializer converts uint64_t timestamps to integer strings on serialization, and back to uint64_t on deserialization.
    • timestamps_as_iso8601 (bool , default False) – If the serializer converts uint64_t timestamps to ISO 8601 strings on serialization, and back to uint64_t on deserialization.

deserialize(self, bytes obj_bytes)

Deserialize the given MessagePack specification bytes to an object.

  • Parameters: obj_bytes (bytes) – The object bytes to deserialize.
  • Return type: Instrument
  • Raises: RuntimeError – If obj_bytes cannot be deserialized.

serialize(self, obj) → bytes

Serialize the given object to MessagePack specification bytes.

  • Parameters: obj (object) – The object to serialize.
  • Return type: bytes
  • Raises: RuntimeError – If obj cannot be serialized.

timestamps_as_iso8601

If the serializer converts timestamp int64_t to ISO 8601 strings.

  • Returns: bool

timestamps_as_str

If the serializer converts timestamp int64_t to integer strings.

  • Returns: bool

class Serializer

Bases: object

Serializer()

The base class for all serializers.

WARNING

This class should not be used directly, but through a concrete subclass.

deserialize(self, bytes obj_bytes)

Abstract method (implement in subclass).

serialize(self, obj) → bytes

Abstract method (implement in subclass).

register_serializable_type(type cls: type, to_dict: Callable[[Any], dict[str, Any]], from_dict: Callable[[dict[str, Any]], Any]) → void

Register the given type with the global serialization type maps.

The type will also be registered as an external publishable type and will be published externally on the message bus unless also added to the MessageBusConfig.types_filter.

  • Parameters:
    • cls (type) – The type to register.
    • to_dict (Callable [ *[*Any ] , dict *[*str , Any ] ]) – The delegate to instantiate a dict of primitive types from an object.
    • from_dict (Callable [ *[*dict *[*str , Any ] ] , Any ]) – The delegate to instantiate an object from a dict of primitive types.
  • Raises:
    • TypeError – If to_dict or from_dict are not of type Callable.
    • KeyError – If type already registered with the global type maps.