msgpack
msgpack
Section titled “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.
Import
Section titled “Import”use std::core::msgpackHex-Encoded Variants
Section titled “Hex-Encoded Variants”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),}Byte-Array Variants
Section titled “Byte-Array Variants”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),}