Standard library · Encoding and data

encoding/msgpack

The compact binary MessagePack format for dynamic values.

import encoding.msgpack
On this page
  1. Types
  2. Functions

MessagePack holds the same data as JSON in a shorter binary form that is quicker to read; it suits sending data between services or putting it in a cache. This package turns a value into a byte buffer and back.

Types

MsgpackOptions

struct MsgpackOptions
The serialising options.
NameTypeDescription
use_str8bool = trueUse the str8 format for short strings; true by default. Very old implementations do not know that format.
encoding-msgpack-Marshal.salam
import encoding.msgpack
import encoding.value
import encoding.encerr
import encoding
import compress

func main:
    mut v := value.NewValueObject()
    value.ObjectSet(v, "id", value.ValueInt(7))
    value.ObjectSet(v, "ok", value.ValueBool(true))
    b := msgpack.Marshal(v)
    println compress.BufLen(b), compress.BufToBase64(b)
    mut err := encerr.NoError()
    back := msgpack.Unmarshal(b, err)
    println encerr.IsError(err), value.Equal(v, back), value.AsInt(value.ObjectGet(back, "id"))
    back2 := msgpack.UnmarshalBytes(b.data, b.len, err)
    println value.Equal(v, back2)
    o := msgpack.MsgpackOptions {}
    println compress.BufLen(msgpack.MarshalWith(v, o))
    println encoding.HexEncode(compress.BufToStrUnsafe(msgpack.Marshal(value.ValueInt(5))))
end
Output9 gqJpZAeib2vD false true 7 true 9 05

Functions

MarshalWith

func MarshalWith(v: value.Value, opts: MsgpackOptions): compress.ByteBuf
Turns a value into MessagePack bytes with the given options.

The example for this item is shown with the ones above: encoding-msgpack-Marshal.salam

Marshal

func Marshal(v: value.Value): compress.ByteBuf
Turns a value into MessagePack bytes.

The example for this item is shown with the ones above: encoding-msgpack-Marshal.salam

UnmarshalBytes

func UnmarshalBytes(p: void*, n: int, err &: encerr.EncodingError): value.Value
Reads n bytes of MessagePack from memory.

The example for this item is shown with the ones above: encoding-msgpack-Marshal.salam

Unmarshal

func Unmarshal(b: compress.ByteBuf, err &: encerr.EncodingError): value.Value
Turns a MessagePack buffer into a value; err is filled in when the data is wrong.

The example for this item is shown with the ones above: encoding-msgpack-Marshal.salam