MessagePack Serializer

MessagePack-based network packet serializer module.

The MessagePack is an alternative representation of the JSON data models.

See also

Optional Dependencies

Explains how to install msgpack extra.

class easynetwork.serializers.msgpack.MessagePackSerializer

Bases: FileBasedPacketSerializer[Any, Any]

A serializer built on top of the msgpack module.

Needs msgpack extra dependencies.

__init__(packer_config: MessagePackerConfig | None = None, unpacker_config: MessageUnpackerConfig | None = None, *, limit: int = DEFAULT_SERIALIZER_LIMIT, debug: bool = False) None
Parameters:
final serialize(packet: Any) bytes

Returns the MessagePack representation of the Python object packet.

Roughly equivalent to:

def serialize(self, packet):
    return msgpack.packb(packet)
Parameters:

packet (Any) – The Python object to serialize.

Returns:

a byte sequence.

Return type:

bytes

final deserialize(data: bytes) Any

Creates a Python object representing the raw MessagePack packet from data.

Roughly equivalent to:

def deserialize(self, data):
    return msgpack.unpackb(data)
Parameters:

data (bytes) – The byte sequence to deserialize.

Raises:
Returns:

the deserialized Python object.

Return type:

Any

final dump_to_file(packet: Any, file: IO[bytes]) None

Write the MessagePack representation of packet to file.

Roughly equivalent to:

def dump_to_file(self, packet, file):
    msgpack.pack(packet, file)
Parameters:
final load_from_file(file: IO[bytes]) Any

Read from file to deserialize the raw MessagePack packet.

Roughly equivalent to:

def load_from_file(self, file):
    return msgpack.unpack(file)
Parameters:

file (IO[bytes]) – The binary file to read from.

Returns:

the deserialized Python object.

Return type:

Any

Configuration

class easynetwork.serializers.msgpack.MessagePackerConfig

Bases: object

A dataclass with the Packer options.

See msgpack.Packer for details.

default: Callable[[Any], Any] | None = None
use_single_float: bool = False
use_bin_type: bool = True
datetime: bool = False
strict_types: bool = False
unicode_errors: str = 'strict'
__init__(*, default: Callable[[Any], Any] | None = None, use_single_float: bool = False, use_bin_type: bool = True, datetime: bool = False, strict_types: bool = False, unicode_errors: str = 'strict') None
class easynetwork.serializers.msgpack.MessageUnpackerConfig

Bases: object

A dataclass with the Unpacker options.

See msgpack.Unpacker for details.

raw: bool = False
use_list: bool = True
timestamp: int = 0
strict_map_key: bool = True
unicode_errors: str = 'strict'
object_hook: Callable[[dict[Any, Any]], Any] | None = None
object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = None
ext_hook: Callable[[int, bytes], Any] | None = None
__init__(*, raw: bool = False, use_list: bool = True, timestamp: int = 0, strict_map_key: bool = True, unicode_errors: str = 'strict', object_hook: Callable[[dict[Any, Any]], Any] | None = None, object_pairs_hook: Callable[[list[tuple[Any, Any]]], Any] | None = None, ext_hook: Callable[[int, bytes], Any] | None = None) None