Skip to content

msgpack

The msgpack module encodes and decodes values using the MessagePack binary format. It supports two representations: a hex-encoded string (text-safe) and a raw Array<int> byte array.

use std::core::msgpack

msgpack::encode(value) -> Result<string, string>

Section titled “msgpack::encode(value) -> Result<string, string>”

Encode any value as a MessagePack hex string.

match msgpack::encode({ name: "Alice", age: 30 }) {
Ok(hex) => print(hex),
Err(e) => print(f"encode error: {e}"),
}

msgpack::decode(data: string) -> Result<_, string>

Section titled “msgpack::decode(data: string) -> Result<_, string>”

Decode a hex-encoded MessagePack string back to a value.

let encoded = msgpack::encode(42)
match encoded {
Ok(hex) => {
match msgpack::decode(hex) {
Ok(v) => print(v), // 42
Err(e) => print(e),
}
},
Err(e) => print(e),
}

msgpack::encode_bytes(value) -> Result<Array<int>, string>

Section titled “msgpack::encode_bytes(value) -> Result<Array<int>, string>”

Encode any value as raw MessagePack bytes.

let bytes_result = msgpack::encode_bytes([1, 2, 3])

msgpack::decode_bytes(data: Array<int>) -> Result<_, string>

Section titled “msgpack::decode_bytes(data: Array<int>) -> Result<_, string>”

Decode raw MessagePack bytes back to a value.

let bytes = msgpack::encode_bytes("hello")
match bytes {
Ok(b) => match msgpack::decode_bytes(b) {
Ok(v) => print(v),
Err(e) => print(e),
},
Err(e) => print(e),
}
  • json — text-format serialization
  • statestate::serialize / state::deserialize use MessagePack under the hood