encoding/msgpack
The compact binary MessagePack format for dynamic values.
import encoding.msgpack
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 MsgpackOptionsThe serialising options.
| Name | Type | Description |
|---|---|---|
use_str8 | bool = true | Use the str8 format for short strings; true by default. Very old implementations do not know that format. |
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))))
endOutput9 gqJpZAeib2vD
false true 7
true
9
05
Functions
MarshalWith
func MarshalWith(v: value.Value, opts: MsgpackOptions): compress.ByteBufTurns 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.ByteBufTurns 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.ValueReads
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.ValueTurns 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