Serializer

MsgPack serialization utils, wrapped into a namespace class.

Attributes

Classes

Serializer

MsgPack-based (de)serialization utils, wrapped into a namespace class.

This class has no value being instantiated: it merely acts as a namespace to pack together encoding and decoding utils to convert data to and from MsgPack dump bytes or binary files.

The MsgPack encoding and decoding capabilities are enhanced to add support for the following non-standard object types: - numpy arrays and scalars - torch tensors (that are always loaded on CPU) - tuples (which would otherwise be converted to lists)

Functions

dump classmethod
dump(obj, path)

Serialize data into a MsgPack binary dump file.

Parameters:

Name Type Description Default
obj Any

Data that needs encoding.

required
path str

Path to the created dump file.

required
Source code in fedbiomed/common/serializer.py
@classmethod
def dump(cls, obj: Any, path: str) -> None:
    """Serialize data into a MsgPack binary dump file.

    Args:
        obj: Data that needs encoding.
        path: Path to the created dump file.
    """
    with open(path, "wb") as file:
        msgpack.pack(obj, file, default=cls._default, strict_types=True)
dumps classmethod
dumps(obj, write_to=None)

Serialize data into MsgPack-encoded bytes.

Parameters:

Name Type Description Default
obj Any

Data that needs encoding.

required

Returns:

Type Description
bytes

MsgPack-encoded bytes that contains the input data.

Source code in fedbiomed/common/serializer.py
@classmethod
def dumps(cls, obj: Any, write_to: Optional[str] = None) -> bytes:
    """Serialize data into MsgPack-encoded bytes.

    Args:
        obj: Data that needs encoding.

    Returns:
        MsgPack-encoded bytes that contains the input data.
    """
    ser = msgpack.packb(obj, default=cls._default, strict_types=True)

    if write_to:
        with open(write_to, "wb") as file:
            file.write(ser)
            file.close()

    return ser
load classmethod
load(path)

Load serialized data from a MsgPack dump file.

Parameters:

Name Type Description Default
path str

Path to a MsgPack file, the contents of which to decode.

required

Returns:

Type Description
Any

Data loaded and decoded from the target file.

Source code in fedbiomed/common/serializer.py
@classmethod
def load(cls, path: str) -> Any:
    """Load serialized data from a MsgPack dump file.

    Args:
        path: Path to a MsgPack file, the contents of which to decode.

    Returns:
        Data loaded and decoded from the target file.
    """
    with open(path, "rb") as file:
        return msgpack.unpack(
            file, object_hook=cls._object_hook, strict_map_key=False
        )
loads classmethod
loads(data)

Load serialized data from a MsgPack-encoded string.

Parameters:

Name Type Description Default
data bytes

MsgPack-encoded bytes that needs decoding.

required

Returns:

Type Description
Any

Data loaded and decoded from the input bytes.

Source code in fedbiomed/common/serializer.py
@classmethod
def loads(cls, data: bytes) -> Any:
    """Load serialized data from a MsgPack-encoded string.

    Args:
        data: MsgPack-encoded bytes that needs decoding.

    Returns:
        Data loaded and decoded from the input bytes.
    """
    return msgpack.unpackb(
        data, object_hook=cls._object_hook, strict_map_key=False
    )