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) -> bytes fails MsgpackError

Encode a JSON string to MessagePack binary.

Fails with MsgpackError.Encode 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) -> string fails MsgpackError

Decode a MessagePack bytes buffer to a JSON string.

Fails with MsgpackError.Decode on invalid input or deserialization failure.

Function encode

pub fn encode<T: Serializable>(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<T: Serializable>(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.

Types

Enum MsgpackError

A conversion the codec refused, with its reason.

Variants

Encode(string)

The JSON text was invalid or could not be encoded.

Decode(string)

The bytes were not valid MessagePack or could not become JSON.