Welcome to AMsgPack’s documentation!¶
It’s like JSON. but fast and small.
>>> from amsgpack import packb, unpackb
>>> packb({"compact": True, "schema": 0})
b'\x82\xa7compact\xc3\xa6schema\x00'
>>> unpackb(b'\x82\xa7compact\xc3\xa6schema\x00')
{'compact': True, 'schema': 0}
- class amsgpack.Ext(code, data)¶
Ext type from MessagePack specification
>>> from amsgpack import Ext, packb >>> packb(Ext(code=95, data=b'amsgpack')) b'\xd7_amsgpack'
- code¶
Ext code (int)
- data¶
Ext data (bytes)
- to_datetime()¶
Converts
datatodatetime.datetime
- to_timestamp()¶
Converts
datatoamsgpack.Timestamp
- class amsgpack.FileUnpacker(file, read_size, tuple=False, ext_hook=None)¶
Iteratively unpack binary stream to python objects:
>>> from amsgpack import FileUnpacker >>> from io import BytesIO >>> for data in FileUnpacker(BytesIO(b'\x00\x01\x02')): ... print(data) ... 0 1 2
- class amsgpack.Packer(default=None)¶
Class for holding
defaultcallback forunpackb()to use. Theamsgpack.packbfunction is created using:packb = Packer().packb
Default callback example:
>>> from typing import Any >>> from amsgpack import Ext, Packer >>> from array import array >>> >>> def default(value: Any) -> Ext: ... if isinstance(value, array): ... return Ext(1, value.tobytes()) ... raise ValueError(f"Unserializable object: {value}") ... >>> packb = Packer(default=default).packb >>> packb(array('I', [0xBA, 0xDE])) b'\xd7\x01\xba\x00\x00\x00\xde\x00\x00\x00'
- packb(obj, /)¶
Serialize
objto a MessagePack formattedbytes.
- class amsgpack.Raw(data)¶
Raw type for
packb(). When packer seesRawtype, it inserts itsdataas is.>>> from amsgpack import Raw, packb >>> packb(Raw(b'Hello')) b'Hello'
- data¶
Raw data (bytes)
- class amsgpack.Timestamp(seconds, nanoseconds)¶
Timestamp extension type from MessagePack specification:
>>> from amsgpack import Timestamp, packb >>> ts = Timestamp(seconds=1752955664) >>> ts Timestamp(seconds=1752955664, nanoseconds=0) >>> packb(ts) b'\xd6\xffh{\xfb\x10' >>> unpackb(packb(ts)) datetime.datetime(2025, 7, 19, 20, 7, 44, tzinfo=datetime.timezone.utc)
See
ext_hookon how to returnTimestampinstead ofdatetime.datetime- nanoseconds¶
32-bit unsigned int
- seconds¶
64-bit signed int
- class amsgpack.Unpacker(tuple=False, ext_hook=None)¶
Unpack bytes to python objects.
The optional tuple argument tells the
Unpackerto output sequences astupleinstead oflist. Theamsgpack.unpackbfunction is created using:unpackb = Unpacker().unpackb
ext_hook example:
>>> from amsgpack import Ext, Unpacker >>> from array import array >>> >>> def ext_hook(ext: Ext): ... if ext.code == 1: ... return array("I", ext.data) ... return ext.default() ... >>> Unpacker(ext_hook=ext_hook).unpackb( ... b"\xd7\x01\xba\x00\x00\x00\xde\x00\x00\x00" ... ) array('I', [186, 222])
- feed(bytes, /)¶
Append
bytesto internal queue.
- reset()¶
Cleans up internal queue, that was filled by
feed()method and and cleans up stack, that might’ve been filled by__next__()
- unpackb(data, /)¶
Deserialize
data(abytesobject) to a Python object. By calling ‘__next__’ one time and ensuring there’s no more data
- amsgpack.packb(obj, /)¶
Serialize
objto a MessagePack formattedbytes.
- amsgpack.unpackb(data, /)¶
Deserialize
data(abytesobject) to a Python object. By calling ‘__next__’ one time and ensuring there’s no more data