Skip to main content

Compact MessagePack serialization for CircuitPython, MicroPython, and CPython — delegates to the native C module when available.

Project description

chumicro-msgpack

Compact MessagePack serialization — much smaller than JSON.

Turns Python dicts, lists, and values into binary bytes, typically 30–50% smaller than JSON. Good for NVM storage, serial protocols, and anywhere bytes matter. On CircuitPython boards with the native msgpack C module, everything delegates to the built-in — the pure-Python code is never loaded, saving ~700 bytes of heap.


Part of the ChuMicro family — small, focused Python libraries for microcontrollers and laptops. See all libraries.

Works on CircuitPython, MicroPython, and CPython.

Installation

CircuitPython (circup)

circup is CircuitPython's package manager — it uses bundles to find third-party packages. Register the ChuMicro bundle once, then install by name:

circup bundle-add ChuMicro/ChuMicro-Bundle
circup install chumicro-msgpack

MicroPython (mip)

mpremote mip install github:ChuMicro/ChuMicro-Bundle/chumicro_msgpack

Want pre-compiled .mpy bytecode? Add mpy6/ before the package name for faster startup and lower RAM usage on boards with mpy format v6 (MicroPython 1.24+):

mpremote mip install github:ChuMicro/ChuMicro-Bundle/mpy6/chumicro_msgpack

CPython (pip)

pip install chumicro-msgpack
Experimental (pre-release) versions and channel switching

Pre-release builds are published automatically when a library version is bumped. Do not register both bundles simultaneously — circup may pick either version for a given package.

# CircuitPython — switch to experimental
circup bundle-remove ChuMicro/ChuMicro-Bundle              # skip if never added
circup bundle-add ChuMicro/ChuMicro-Bundle-Experimental
circup install chumicro-msgpack

# MicroPython
mpremote mip install github:ChuMicro/ChuMicro-Bundle-Experimental/chumicro_msgpack

# CPython
pip install chumicro-msgpack-experimental

Quick example

from chumicro_msgpack import packb, unpackb

settings = {0: "MyNetwork", 1: "secret", 2: True}

data = packb(settings)       # compact binary bytes
print(len(data))             # much smaller than JSON

restored = unpackb(data)
print(restored)              # {0: 'MyNetwork', 1: 'secret', 2: True}

What's included

Stream-based API (preferred on microcontrollers)

Symbol Description
pack(obj, stream) Pack an object directly to a writable stream — no intermediate buffer
unpack(stream) Unpack one object from a readable stream

Bytes-based API

Symbol Description
packb(obj) Pack a Python object to msgpack bytes (allocates a temporary buffer)
unpackb(data) Unpack msgpack bytes (bytes, bytearray, or memoryview) to a Python object

Use pack/unpack when writing to files, sockets, or NVM. Use packb/unpackb when you need the encoded bytes in memory (e.g., to measure length before framing).

Supported types

Python type msgpack format
None nil
True / False bool
int (−2³¹ to 2³²−1) fixint, int8/16/32, uint8/16/32
float float32
str fixstr, str8, str16
bytes / bytearray bin8, bin16
list / tuple fixarray, array16
dict fixmap, map16

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

Platform support

Runtime Implementation
CircuitPython (hardware) Delegates to the native C msgpack module; pure-Python code is never loaded
CircuitPython (unix port) Pure-Python encoder/decoder (native module not compiled in)
MicroPython Pure-Python encoder/decoder
CPython Pure-Python encoder/decoder

Examples

Example What it shows
packb_basic.py Pack and unpack a settings dict
packb_size_comparison.py Compare msgpack vs JSON size for the same dict
stream_roundtrip.py Use the stream-based pack / unpack API with BytesIO
circuitpython_nvm_settings.py Store and load settings in non-volatile memory (hardware)

Docs

📖 Stable docs · Experimental docs

Find this library

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chumicro_msgpack-0.1.23.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chumicro_msgpack-0.1.23-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file chumicro_msgpack-0.1.23.tar.gz.

File metadata

  • Download URL: chumicro_msgpack-0.1.23.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for chumicro_msgpack-0.1.23.tar.gz
Algorithm Hash digest
SHA256 24058036e39b4d710d74ddfdd51c0a3dae9e7aa5b26164496ce4ac0cbf120e73
MD5 9ad311f15adc8b6db2cf93d73ae26e57
BLAKE2b-256 f8694d16ca47989a48b024350340f268b2af5d4d323ecb9a4a31cfa63323ec16

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_msgpack-0.1.23.tar.gz:

Publisher: promote.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file chumicro_msgpack-0.1.23-py3-none-any.whl.

File metadata

File hashes

Hashes for chumicro_msgpack-0.1.23-py3-none-any.whl
Algorithm Hash digest
SHA256 329da318203030696b6d8c45c49b9b140b39e18ff0814878b118b94e825fb22b
MD5 bbf94088c7bd5bff772c913fdee8dc55
BLAKE2b-256 a250ae1ffed5ff48a235445e18a303c8ada08105efc403652a18b19a58a55de1

See more details on using hashes here.

Provenance

The following attestation bundles were made for chumicro_msgpack-0.1.23-py3-none-any.whl:

Publisher: promote.yml on ChuMicro/ChuMicro

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page