std/msgpack provides a compact binary alternative to JSON. Each value is packed into a binary string that preserves its type. It is the right fit for on-disk formats and wire protocols where every byte counts.
// MessagePack binary serialization — compact alternative to JSON // Serialización binaria MessagePack — alternativa compacta a JSON import "std/msgpack" fn main() -> int { // Pack individual values 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() // Check types 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)) // Unpack values 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) // Pack an array of msgpack values 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 }
int type: int str type: str bool type: bool_true nil type: nil unpacked int: 42 unpacked str: hello array type: int
How it works
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 header byte is emitted through the integer packer, so msgpack_type reports int for the composed array instead of array — read the length prefix yourself if you need to tell them apart.
The pack/unpack functions operate on Nyx strings (which are byte sequences), making them compatible with network transmission and file storage without additional encoding.