Bases: BaseSerializer
Serializer that wraps the msgspec MessagePack serialization protocol.
Recommended serializer for highspeed applications.
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| class MsgpackSerializer(BaseSerializer):
"""
Serializer that wraps the msgspec MessagePack serialization protocol.
Recommended serializer for highspeed applications.
"""
def __init__(self) -> None:
super().__init__()
self.type = msgpack
codes = dict(NDARRAY_EXT=1)
def loads(self, data: bytearray | memoryview | bytes) -> Any:
return msgpack.decode(self.convert_to_bytes(data), ext_hook=self.ext_decode)
def dumps(self, data: Any) -> bytes:
return msgpack.encode(data, enc_hook=self.default_encode)
@classmethod
def default_encode(cls, obj) -> Any:
"""
Encode types that MessagePack does not support natively, currently numpy arrays.
Parameters
----------
obj: Any
the object to be serialized
Returns
-------
Any
a MessagePack serializable representation of the object
Raises
------
TypeError
if the object cannot be serialized to MessagePack
"""
if "numpy" in globals() and isinstance(obj, numpy.ndarray):
buf = io.BytesIO()
numpy.save(buf, obj, allow_pickle=False) # use .npy. which stores dtype, shape, order, endianness
return msgpack.Ext(MsgpackSerializer.codes["NDARRAY_EXT"], buf.getvalue())
raise TypeError("Given type cannot be converted to MessagePack : {}".format(type(obj)))
@classmethod
def ext_decode(cls, code: int, obj: memoryview) -> Any:
"""
Decode a MessagePack extension type, currently numpy arrays.
Parameters
----------
code: int
the extension type code
obj: memoryview
the payload of the extension type
Returns
-------
Any
the decoded object, or the payload itself for an unknown extension code
Raises
------
ValueError
if a numpy array is encountered but numpy is not installed
"""
if code == MsgpackSerializer.codes["NDARRAY_EXT"]:
if "numpy" in globals():
return numpy.load(io.BytesIO(obj), allow_pickle=False)
else:
raise ValueError("numpy is required to decode numpy array from MessagePack")
return obj
@property
def content_type(self) -> str:
return "application/msgpack"
|
Attributes
codes
class-attribute
instance-attribute
codes = dict(NDARRAY_EXT=1)
Functions
__init__
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| def __init__(self) -> None:
super().__init__()
self.type = msgpack
|
dumps
dumps(data: Any) -> bytes
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| def dumps(self, data: Any) -> bytes:
return msgpack.encode(data, enc_hook=self.default_encode)
|
loads
loads(data: bytearray | memoryview | bytes) -> Any
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| def loads(self, data: bytearray | memoryview | bytes) -> Any:
return msgpack.decode(self.convert_to_bytes(data), ext_hook=self.ext_decode)
|
default_encode
classmethod
default_encode(obj) -> Any
Encode types that MessagePack does not support natively, currently numpy arrays.
Parameters:
| Name |
Type |
Description |
Default |
obj
|
|
the object to be serialized
|
required
|
Returns:
| Type |
Description |
Any
|
a MessagePack serializable representation of the object
|
Raises:
| Type |
Description |
TypeError
|
if the object cannot be serialized to MessagePack
|
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| @classmethod
def default_encode(cls, obj) -> Any:
"""
Encode types that MessagePack does not support natively, currently numpy arrays.
Parameters
----------
obj: Any
the object to be serialized
Returns
-------
Any
a MessagePack serializable representation of the object
Raises
------
TypeError
if the object cannot be serialized to MessagePack
"""
if "numpy" in globals() and isinstance(obj, numpy.ndarray):
buf = io.BytesIO()
numpy.save(buf, obj, allow_pickle=False) # use .npy. which stores dtype, shape, order, endianness
return msgpack.Ext(MsgpackSerializer.codes["NDARRAY_EXT"], buf.getvalue())
raise TypeError("Given type cannot be converted to MessagePack : {}".format(type(obj)))
|
ext_decode
classmethod
ext_decode(code: int, obj: memoryview) -> Any
Decode a MessagePack extension type, currently numpy arrays.
Parameters:
| Name |
Type |
Description |
Default |
code
|
int
|
|
required
|
obj
|
memoryview
|
the payload of the extension type
|
required
|
Returns:
| Type |
Description |
Any
|
the decoded object, or the payload itself for an unknown extension code
|
Raises:
| Type |
Description |
ValueError
|
if a numpy array is encountered but numpy is not installed
|
Source code in repo/hololinked/hololinked/serializers/msgpack.py
| @classmethod
def ext_decode(cls, code: int, obj: memoryview) -> Any:
"""
Decode a MessagePack extension type, currently numpy arrays.
Parameters
----------
code: int
the extension type code
obj: memoryview
the payload of the extension type
Returns
-------
Any
the decoded object, or the payload itself for an unknown extension code
Raises
------
ValueError
if a numpy array is encountered but numpy is not installed
"""
if code == MsgpackSerializer.codes["NDARRAY_EXT"]:
if "numpy" in globals():
return numpy.load(io.BytesIO(obj), allow_pickle=False)
else:
raise ValueError("numpy is required to decode numpy array from MessagePack")
return obj
|