Nyx by Example

MessagePack

std/msgpack provides binary serialization that is roughly 30% smaller than JSON for typical data. Each value is packed into a compact binary string that preserves its type. This is used internally by nyx-kv for its .ndb persistence format.

Code

// MessagePack binary serialization

import "std/msgpack"

fn main() -> int {
    let packed_int: String = msgpack_int(42)
    let packed_str: String = msgpack_str("hello")
    let packed_bool: String = msgpack_bool(true)
    let packed_nil: String = msgpack_nil()

    print("int type:  " + msgpack_type(packed_int))
    print("str type:  " + msgpack_type(packed_str))
    print("bool type: " + msgpack_type(packed_bool))
    print("nil type:  " + msgpack_type(packed_nil))

    let val_int: int = msgpack_unpack_int(packed_int)
    let val_str: String = msgpack_unpack_str(packed_str)
    print("unpacked int: " + int_to_string(val_int))
    print("unpacked str: " + val_str)

    let items: Array = [msgpack_int(1), msgpack_str("two"), msgpack_bool(false)]
    let packed_arr: String = msgpack_array(items)
    print("array type: " + msgpack_type(packed_arr))

    return 0
}

Output

int type:  positive_int
str type:  str
bool type: bool_true
nil type:  nil
unpacked int: 42
unpacked str: hello
array type: array

Explanation

MessagePack encodes each value into a compact binary representation. Small integers (0-127) fit in a single byte, short strings use a 1-byte header, and booleans/nil are always 1 byte. The msgpack_type function inspects the first byte to determine what kind of value is stored.

msgpack_array takes an array of already-packed values and prepends an array header. This lets you build arbitrarily nested structures: pack individual values first, then compose them into arrays.

The pack/unpack functions operate on Nyx strings (which are byte sequences), making them compatible with network transmission and file storage without additional encoding.

← Previous Next →

Source: examples/by-example/44-msgpack.nx