Skip to content

API Reference

chumicro_msgpack

chumicro_msgpack

MessagePack serialization for CircuitPython, MicroPython, and CPython.

Implements a subset of the msgpack specification <https://msgpack.org>_ suitable for embedded use: integers (up to 32-bit), floats (32-bit), strings, bytes, booleans, None, lists, tuples, and dicts.

64-bit integers and floats are not supported, matching CircuitPython's built-in msgpack module limitation.

Public API

  • packb(obj) — pack a Python object to msgpack bytes.
  • unpackb(data) — unpack msgpack bytes to a Python object.
  • pack(obj, stream) — pack to a writable stream.
  • unpack(stream) — unpack one object from a readable stream.

On CircuitPython boards that include the native msgpack module, all four functions delegate to the C implementation. The pure-Python encoder in _pure is never imported, saving ~700 bytes of heap RAM.

pack(obj, stream)

Pack obj to stream in msgpack format.

Parameters:

Name Type Description Default
obj object

Python object to serialize.

required
stream object

Writable stream with a write() method.

required

unpack(stream)

Unpack one object from stream.

Parameters:

Name Type Description Default
stream object

Readable stream with a read() method.

required

Returns:

Type Description
object

Deserialized Python object.

packb(obj)

Pack obj to msgpack bytes.

Allocates a temporary bytearray that grows during encoding, then copies to bytes. For small payloads this is fine; for larger data or tight loops, prefer pack(obj, stream) to write directly to a destination without the intermediate allocation.

Parameters:

Name Type Description Default
obj object

Python object to serialize.

required

Returns:

Type Description
bytes

Msgpack-encoded data.

unpackb(data)

Unpack msgpack data to a Python object.

Parameters:

Name Type Description Default
data bytes | bytearray | memoryview

Msgpack-encoded data.

required

Returns:

Type Description
object

Deserialized Python object.

chumicro_msgpack._pure

chumicro_msgpack._pure

Pure-Python msgpack encoder/decoder with native CircuitPython fallback.

Supports: None, bool, int (32-bit), float (32-bit), str, bytes, bytearray, list, tuple, and dict.

packb(obj)

Pack obj to msgpack bytes.

Allocates a temporary bytearray that grows during encoding, then copies to bytes. For small payloads this is fine; for larger data or tight loops, prefer pack(obj, stream) to write directly to a destination without the intermediate allocation.

Parameters:

Name Type Description Default
obj object

Python object to serialize.

required

Returns:

Type Description
bytes

Msgpack-encoded data.

unpackb(data)

Unpack msgpack data to a Python object.

Parameters:

Name Type Description Default
data bytes | bytearray | memoryview

Msgpack-encoded data.

required

Returns:

Type Description
object

Deserialized Python object.

pack(obj, stream)

Pack obj to stream in msgpack format.

Parameters:

Name Type Description Default
obj object

Python object to serialize.

required
stream object

Writable stream with a write() method.

required

unpack(stream)

Unpack one object from stream.

Parameters:

Name Type Description Default
stream object

Readable stream with a read() method.

required

Returns:

Type Description
object

Deserialized Python object.