Serialization - Loren1166/NautilusTrader- GitHub Wiki

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 | 消息规范序列化器

class MsgSpecSerializer(Serializer):
    """
    Provides a serializer for either the ‘MessagePack’ or ‘JSON’ specifications. 
    提供“MessagePack”或“JSON”规范的序列化器。

    Parameters:
    参数:
        encoding (Callable) – The msgspec encoding type. 
        msgspec 编码类型。
        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. 
        如果序列化器在序列化时将 uint64_t 时间戳转换为整数字符串,在反序列化时转换回 uint64_t,则为 True。
        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. 
        如果序列化器在序列化时将 uint64_t 时间戳转换为 ISO 8601 字符串,在反序列化时转换回 uint64_t,则为 True。
    """
    def __init__(
        self,
        encoding: Callable,
        timestamps_as_str: bool = False,
        timestamps_as_iso8601: bool = False
    ):
        ...

    def deserialize(self, obj_bytes: bytes):
        """
        Deserialize the given MessagePack specification bytes to an object. 
        将给定的 MessagePack 规范字节反序列化为对象。

        Parameters: 
        参数:
            obj_bytes (bytes) – The object bytes to deserialize. 
            要反序列化的对象字节。

        Return type: Instrument
        Raises: RuntimeError – If obj_bytes cannot be deserialized. 
        如果 obj_bytes 无法反序列化,则引发 RuntimeError。
        """
        ...

    def serialize(self, obj) -> bytes:
        """
        Serialize the given object to MessagePack specification bytes. 
        将给定对象序列化为 MessagePack 规范字节。

        Parameters: 
        参数:
            obj (object) – The object to serialize. 
            要序列化的对象。

        Return type: bytes
        Raises: RuntimeError – If obj cannot be serialized. 
        如果 obj 无法序列化,则引发 RuntimeError。
        """
        ...

    @property
    def timestamps_as_iso8601(self) -> bool:
        """
        If the serializer converts timestamp int64_t to ISO 8601 strings. 
        如果序列化器将时间戳 int64_t 转换为 ISO 8601 字符串。

        Returns: bool
        """
        ...

    @property
    def timestamps_as_str(self) -> bool:
        """
        If the serializer converts timestamp int64_t to integer strings. 
        如果序列化器将时间戳 int64_t 转换为整数字符串。

        Returns: bool
        """
        ...

class Serializer | 序列化器

class Serializer:
    """
    The base class for all serializers. 
    所有序列化器的基类。

    WARNING
    This class should not be used directly, but through a concrete subclass. 
    此类不应直接使用,而应通过具体的子类使用。
    """
    def __init__(self):
        ...

    def deserialize(self, obj_bytes: bytes):
        """
        Abstract method (implement in subclass). 
        抽象方法(在子类中实现)。
        """
        ...

    def serialize(self, obj) -> bytes:
        """
        Abstract method (implement in subclass). 
        抽象方法(在子类中实现)。
        """
        ...

Functions | 函数

def register_serializable_type(
    cls: type,
    to_dict: Callable[Any], dict[str, Any](/Loren1166/NautilusTrader-/wiki/Any],-dict[str,-Any),
    from_dict: Callable[dict[str, Any](/Loren1166/NautilusTrader-/wiki/dict[str,-Any), Any]
) -> None:
    """
    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. 
    该类型还将注册为外部可发布类型,并且将在消息总线上外部发布,除非也将其添加到 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. 
        如果 to_dict 或 from_dict 的类型不是 Callable,则引发 TypeError。
        KeyError – If type already registered with the global type maps. 
        如果类型已在全局类型映射中注册,则引发 KeyError。
    """
    ...