March Docs

Msgpack

Msgpack module: MessagePack binary serialization.

Encodes and decodes the MessagePack format (https://msgpack.org/). Compact binary alternative to JSON; supported by most languages.

Types: Value = Null | Bool(Bool) | Int(Int) -- 63-bit signed; see plan for range details | Str(String) -- UTF-8 bytes | Bin(List(Int)) -- raw bytes (values 0-255) | Array(List(Value)) | Map(List((Value, Value)))

Functions: encode(Value) -> List(Int) decode(List(Int)) -> Result(Value, String) decode_all(List(Int)) -> Result(List(Value), String) null/bool/int/str/bin/array/map -- convenience constructors

Note: Float is not supported in v1 (requires float_bits builtin). Note: encode returns List(Int); wrap with Bytes.from_list if needed.

Format byte constants (decimal): 192 = nil 194 = false 195 = true 196..198 = bin8/16/32 202..203 = float32/64 (unsupported) 204..207 = uint8/16/32/64 208..211 = int8/16/32/64 217..219 = str8/16/32 220..221 = array16/32 222..223 = map16/32 128..143 = fixmap 144..159 = fixarray 160..191 = fixstr 0..127 = pos fixint 224..255 = neg fixint

Types

typeValueValue#

Functions

fnarrayarray(xs : List(Value)) : Value do Array(xs) end#

Construct a MessagePack array value.

fnbinbin(bs : List(Int)) : Value do Bin(bs) end#

Construct a MessagePack binary value from a list of byte ints (0-255).

fnboolbool(b : Bool) : Value do Bool(b) end#

Construct a MessagePack boolean value.

fndecodedecode(bs : List(Int)) : Result(Value, String)#

Decode a MessagePack byte list to a Value. Returns Err if the input is empty, truncated, has trailing bytes, or contains an unsupported format byte (float, ext).

    Msgpack.decode([192])                -- Ok(Null)
    Msgpack.decode([42])                 -- Ok(Int(42))
    Msgpack.decode([195])                -- Ok(Bool(true))
    Msgpack.decode([])                   -- Err("unexpected end of input")
fndecode_alldecode_all(bs : List(Int)) : Result(List(Value), String)#

Decode zero or more concatenated MessagePack values from bs. Returns Ok([]) on empty input. Useful for framing protocols.

    Msgpack.decode_all([195, 42, 194])
    -- Ok([Bool(true), Int(42), Bool(false)])
fnencodeencode(v : Value) : List(Int)#

Encode a Value to a list of bytes (0-255) in MessagePack format.

    Msgpack.encode(Msgpack.Null)         -- [192]
    Msgpack.encode(Msgpack.Bool(true))   -- [195]
    Msgpack.encode(Msgpack.Int(42))      -- [42]
    Msgpack.encode(Msgpack.Str("hi"))    -- [162, 104, 105]
fnintint(n : Int) : Value do Int(n) end#

Construct a MessagePack integer value.

fnmapmap(kvs : List((Value, Value))) : Value do Map(kvs) end#

Construct a MessagePack map value from a list of (key, value) pairs.

fnnullnull() : Value do Null end#

Construct a MessagePack null value.

fnstrstr(s : String) : Value do Str(s) end#

Construct a MessagePack string value.