MessagePack API Reference
Binary serialization format -- efficient, compact alternative to JSON
Import
U std/msgpack
Overview
The msgpack module implements the MessagePack binary serialization format. It supports nil, booleans, integers, strings, binary data, arrays, and maps. Values use a 24-byte tagged representation compatible with the JSON module.
Value Representation
All values use the same 24-byte layout as json.vais:
[tag: i64, data: i64, extra: i64]
| Tag | Type | Data | Extra |
|---|---|---|---|
| 0 | nil | - | - |
| 1 | bool | 0=false, 1=true | - |
| 2 | integer | i64 value | - |
| 3 | string | pointer | length |
| 4 | binary | pointer | length |
| 5 | array | pointer to array struct | - |
| 6 | map | pointer to map struct | - |
Wire Format
The module encodes/decodes the standard MessagePack wire format:
0x00-0x7f: positive fixint0x80-0x8f: fixmap0x90-0x9f: fixarray0xa0-0xbf: fixstr0xc0: nil0xc2/0xc3: false/true0xc4-0xc6: bin 8/16/320xd9-0xdb: str 8/16/320xdc-0xdd: array 16/320xde-0xdf: map 16/320xe0-0xff: negative fixint
Key Functions
Buffer Management
F pb_buf_new() -> i64 # Create encoding buffer
Value Constructors
Create tagged values for encoding:
F msgpack_nil() -> i64 # Create nil value
F msgpack_bool(v: i64) -> i64 # Create bool value
F msgpack_int(v: i64) -> i64 # Create integer value
F msgpack_str(s: str) -> i64 # Create string value
F msgpack_bin(ptr: i64, len: i64) -> i64 # Create binary value
Encoding/Decoding
F msgpack_encode(value: i64) -> i64 # Encode value to bytes
F msgpack_decode(data: i64, len: i64) -> i64 # Decode bytes to value
Example
U std/msgpack
F main() {
# Create and encode values
val := msgpack_int(42)
encoded := msgpack_encode(val)
}