Module std::encoding::msgpack

MessagePack serialization.

Encode and decode values using the MessagePack binary format. Supports conversion between JSON strings and MessagePack bytes, as well as encoding individual typed values.

Examples

import std.encoding.msgpack;

fn main() {
    let packed = msgpack.from_json("{\"x\": 1}") handle error {
        println(f"operation failed: {error}");
        return;
    };
    println(packed.len());
    let json = msgpack.to_json(packed) handle error {
        println(f"operation failed: {error}");
        return;
    };
    println(json);
}

Contents

Functions

Function from_json

pub fn from_json(json: string) -> Result<bytes, string>

Encode a JSON string to MessagePack binary.

Returns Err(reason) on invalid JSON or serialization failure. The codec's own reason slot decides success, not the length of the result, so a legitimately short encoding is never mistaken for a failure.

Function to_json

pub fn to_json(data: bytes) -> Result<string, string>

Decode a MessagePack bytes buffer to a JSON string.

Returns Err(reason) on invalid input or deserialization failure.

Function encode

pub fn encode(value: T) -> bytes

Encode any data value as MessagePack bytes. Records encode as objects keyed by field name in declaration order.

Function decode

pub fn decode(data: bytes) -> Result<T, wire.DecodeError>

Decode MessagePack bytes as T. A failure names the path of the value that did not fit, for example Type: .tags[1]: expected string, found integer.