API Reference¶
- ormsgpack.packb(obj, /, default=None, option=None)¶
Serializes a Python object to a binary object in MessagePack format.
The global interpreter lock (GIL) is held for the duration of the call.
The serialization is based on the mappings defined in section Types.
- Parameters:
obj (Any) – The object to serialize
default (Callable[[Any], Any] | None) – if set, a callable object for serializing objects that are not serializable.
defaultis called with one argument, an object to serialize, and its return value is used as the serializable representation of the object. If the return value is not serializable,defaultis called recursively, up to 254 timesoption (int | None) – if set, one of the
OPT_*integer constants or a combination of them using the bitwise OR operator
- Raises:
MsgpackEncodeError – if an object is not serializable
MsgpackEncodeError – if a
strinstance contains surrogate code points andOPT_REPLACE_SURROGATESis not specifiedMsgpackEncodeError – if a
dictkey is not astrinstance andOPT_NON_STR_KEYSis not specifiedMsgpackEncodeError – if
defaultis called recursively more than 254 timesMsgpackEncodeError – if an object contains a circular reference
MsgpackEncodeError – if a
datetime.datetime.tzinfoattribute is of an unsupported type
- Return type:
- ormsgpack.unpackb(obj, /, *, ext_hook=None, option=None)¶
Deserializes a binary object in MessagePack format to a Python object.
The global interpreter lock (GIL) is held for the duration of the call.
The deserialization is based on the following mappings:
nil is deserialized as the
Noneobjectboolean objects are deserialized as
boolinstancesinteger objects are deserialized as
intinstancesfloat objects are deserialized as
floatinstancesstring objects are deserialized as
strinstancesbinary objects are deserialized as
bytesinstancesarray objects are deserialized as
tupleinstances, if the object is a map key, and aslistinstances otherwisemap objects are deserialized as
dictinstancestimestamp extension objects are deserialized as UTC
datetime.datetimeinstances, ifOPT_DATETIME_AS_TIMESTAMP_EXTis specified
- Parameters:
obj (bytes | bytearray | memoryview) – The object to deserialize
ext_hook (Callable[[int, bytes], Any] | None) – if set, a callable object for deserializing extension types.
ext_hookis called with two arguments, the extension type and value, and its return value is used as the deserialized objectoption (int | None) – if set,
OPT_DATETIME_AS_TIMESTAMP_EXT,OPT_NON_STR_KEYSor their combination using the bitwise OR operator
- Raises:
MsgpackDecodeError – if the object is of an invalid type or is not valid MessagePack
MsgpackDecodeError – if a map key is not a string and
OPT_NON_STR_KEYSis not specified
- Return type:
Any
- exception ormsgpack.MsgpackDecodeError¶
a subclass of
ValueError
- ormsgpack.OPT_DATETIME_AS_TIMESTAMP_EXT¶
In
packb(), serialize awaredatetime.datetimeinstances as timestamp extension objectsIn
unpackb(), deserialize timestamp extension objects to UTCdatetime.datetimeinstances
- ormsgpack.OPT_NAIVE_UTC¶
Serialize naive
datetime.datetimeobjects andnumpy.datetime64objects as UTC. This has no effect on awaredatetime.datetimeobjects.>>> import ormsgpack, datetime >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0), ... ) b'\xb31970-01-01T00:00:00' >>> ormsgpack.unpackb(_) '1970-01-01T00:00:00' >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0), ... option=ormsgpack.OPT_NAIVE_UTC, ... ) b'\xb91970-01-01T00:00:00+00:00' >>> ormsgpack.unpackb(_) '1970-01-01T00:00:00+00:00'
- ormsgpack.OPT_NON_STR_KEYS¶
In
packb(), serializedictkeys of typeNone,bool,int,float,str,bytes,datetime.date,datetime.time,datetime.datetime,enum.Enum, anduuid.UUID. All options other than the passthrough ones are supported.dictkeys of unsupported types are not handled usingdefaultand result inMsgpackEncodeErrorbeing raised.In
unpackb(), deserialize map keys of type other than string. Be aware that this option is considered unsafe and disabled by default in msgpack due to the possibility of a Hash DoS.>>> import ormsgpack >>> >>> ormsgpack.packb( ... {0: [1, 2], 1.0: [3, 4]}, ... ) TypeError: Dict key must be str >>> ormsgpack.packb( ... {0: [1, 2], 1.0: [3, 4]}, ... option=ormsgpack.OPT_NON_STR_KEYS, ... ) b'\x82\x00\x92\x01\x02\xcb?\xf0\x00\x00\x00\x00\x00\x00\x92\x03\x04' >>> ormsgpack.unpackb(_, option=ormsgpack.OPT_NON_STR_KEYS) {0: [1, 2], 1.0: [3, 4]}
This option is not compatible with
OPT_SORT_KEYS.
- ormsgpack.OPT_OMIT_MICROSECONDS¶
Do not serialize the microsecond component of
datetime.datetime,datetime.timeandnumpy.datetime64instances.>>> import ormsgpack, datetime >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0, 1), ... ) b'\xba1970-01-01T00:00:00.000001' >>> ormsgpack.unpackb(_) '1970-01-01T00:00:00.000001' >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0, 1), ... option=ormsgpack.OPT_OMIT_MICROSECONDS, ... ) b'\xb31970-01-01T00:00:00' >>> ormsgpack.unpackb(_) '1970-01-01T00:00:00'
- ormsgpack.OPT_PASSTHROUGH_BIG_INT¶
Enable passthrough of
intinstances smaller than -9223372036854775807 or larger than 18446744073709551615 todefault.>>> import ormsgpack >>> def default(obj): ... if isinstance(obj, int): ... return ormsgpack.Ext(0, obj.to_bytes(9)) ... raise TypeError ... >>> ormsgpack.packb(2**65) TypeError: Integer exceeds 64-bit range >>> ormsgpack.packb( ... 2**65, ... option=ormsgpack.OPT_PASSTHROUGH_BIG_INT, ... default=default, ... ) b'\xc7\t\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00'
- ormsgpack.OPT_PASSTHROUGH_DATACLASS¶
Enable passthrough of dataclasses to
default.
- ormsgpack.OPT_PASSTHROUGH_DATETIME¶
Enable passthrough of
datetime.datetime,datetime.date, anddatetime.timeinstances todefault.
- ormsgpack.OPT_PASSTHROUGH_ENUM¶
Enable passthrough of enum members to
default.
- ormsgpack.OPT_PASSTHROUGH_SUBCLASS¶
Enable passthrough of subclasses of
str,int,dictandlisttodefault.
- ormsgpack.OPT_REPLACE_SURROGATES¶
Serialize
strinstances that contain surrogate code points by replacing the surrogates with the?character.
- ormsgpack.OPT_SERIALIZE_NUMPY¶
Serialize instances of numpy types.
- ormsgpack.OPT_SERIALIZE_PYDANTIC¶
Serialize
pydantic.BaseModelinstances.
- ormsgpack.OPT_SORT_KEYS¶
Serialize
dictkeys and pydantic model fields in sorted order. The default is to serialize in an unspecified order.This can be used to ensure the order is deterministic for hashing or tests. It has a substantial performance penalty and is not recommended in general.
>>> import ormsgpack >>> ormsgpack.packb({"b": 1, "c": 2, "a": 3}) b'\x83\xa1b\x01\xa1c\x02\xa1a\x03' >>> ormsgpack.packb({"b": 1, "c": 2, "a": 3}, option=ormsgpack.OPT_SORT_KEYS) b'\x83\xa1a\x03\xa1b\x01\xa1c\x02'
The sorting is not collation/locale-aware:
>>> import ormsgpack >>> ormsgpack.packb({"a": 1, "ä": 2, "A": 3}, option=ormsgpack.OPT_SORT_KEYS) b'\x83\xa1A\x03\xa1a\x01\xa2\xc3\xa4\x02'
This option is not supported for dataclasses.
- ormsgpack.OPT_UTC_Z¶
Serialize a UTC timezone on
datetime.datetimeandnumpy.datetime64instances asZinstead of+00:00.>>> import ormsgpack, datetime >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ... ) b'\xb91970-01-01T00:00:00+00:00' >>> ormsgpack.packb( ... datetime.datetime(1970, 1, 1, 0, 0, 0, tzinfo=datetime.timezone.utc), ... option=ormsgpack.OPT_UTC_Z, ... ) b'\xb41970-01-01T00:00:00Z'