pkg

ctxcode/valk-msgpack

Pure-Valk MessagePack encoder and decoder with typed class/struct support

github.com/ctxcode/valk-msgpack ↗
0.2.0 latest Valk ≥ 0.9.1

Install

vman install github.com/ctxcode/valk-msgpack

Adds the package to your valk.json. To allow only some versions, add them: vman install github.com/ctxcode/valk-msgpack 0.2.x

valk-msgpack

Pure-Valk MessagePack encoder and decoder implementing the full
MessagePack specification, with
typed class/struct serialization powered by Valk $tag reflection. No C or OS-package
dependencies.

Requires Valk 0.9.1 or newer.

Install

vman install github.com/ctxcode/valk-msgpack

Example

use msgpack

class User {
    id: u64 $tag(msgpack: "user_id")
    name: String `msgpack: "full_name"`
    secret: String ("hidden") $tag(msgpack: "-")
    bio: ?String (null) $tag(omitempty)
    tags: Array[String] (.{}) $tag(omitempty)
    created_at: msgpack.Timestamp
}

fn main() {
    let ts = msgpack.Timestamp.new(1732114390, 500_000_000) ! panic(E.message)
    let user = User {
        id: 42
        name: "Ada Lovelace"
        secret: "never-encoded"
        tags: Array[String]{ "math", "valk" }
        created_at: ts
    }

    let bytes = msgpack.encode(user)
    let decoded = msgpack.decode_to[User](bytes) ! panic(E.message)
    println(decoded.name)
}

Dynamic `Value` API

When the schema is not fixed, msgpack.decode returns a msgpack.Value covering every
MessagePack type (nil, bool, int, uint, float, str, bin, array, map, ext):

let val = msgpack.decode(bytes) ! panic(E.message)
println(val["user_id"].uint)
println(val["tags"][0].str)

// Arbitrary non-string map keys are preserved in order
let item = val.get_key(msgpack.Value.from_int(1)) ! panic(E.message)

Convert a dynamic Value into a typed Valk value with val.to_type[T](), or convert any
typed value into a Value with msgpack.from(x).

Field Tags & Custom Hooks

Classes and structs support Valk field tags (both $tag(...) and backtick syntax):

  • $tag(msgpack: "name") / `msgpack: "name"` : renames the MessagePack map key.
  • $tag(msgpack: "-"): skips the field on encode and uses its default value on decode.
  • $tag(omitempty): omits zero, false, null, empty string/binary, or empty collection
    values on encode; accepts missing or nil values on decode when a default exists.

User types can also customize serialization by implementing:

  • to_msgpack() msgpack.Value or write_msgpack(out: ByteBuffer) for encoding.
  • static fn from_msgpack(value: msgpack.Value) SELF !msgpack.Error for decoding.

Wire Format & Features

  • Shortest-form encoding: positive/negative fixint, uint 8/16/32/64, int 8/16/32/64,
    float 32/64, fixstr/str 8/16/32, bin 8/16/32, fixarray/array 16/32,
    fixmap/map 16/32, fixext 1/2/4/8/16, and ext 8/16/32.
  • Timestamp extension (ext type -1): full 32-bit, 64-bit, and 96-bit encoding and
    decoding via msgpack.Timestamp, plus automatic conversion for valk.time.DateTime.
  • Streaming & multi-object: encode_into(value, writer) for ByteBuffer and any
    io.Writer, Encoder for building streams, Decoder for reading objects one by one,
    and decode_all for decoding concatenated objects.
  • Safe decoding: strict bounds checking before allocating payloads or containers,
    0xc1 rejection, trailing-byte detection, and recursion depth limiting (MAX_DEPTH = 128).

Errors

msgpack.Error reports typed failures with a message and byte offset:

  • truncated: input ended before a value or payload finished.
  • invalid_format: invalid format byte (0xc1) or malformed timestamp extension.
  • depth_limit: container nesting exceeded MAX_DEPTH.
  • type_mismatch: value cannot be converted to the target type or overflows its range.
  • trailing_data: extra bytes remained after decoding a single value.
  • missing_field: a required field on a target class or struct was absent.

Documentation

Full API reference is generated in docs/api.md and docs/api-full.md.

Development

make lint checks sources with --deny-warnings, make test runs the test suite,
make example runs the example, and make docs regenerates the API documentation.