msgpack
The msgpack library (lua-cmsgpack) packs Lua values into a compact binary MessagePack string and back. On error the functions return nil and an error text.
Packing rules are the same as for database: nesting up to 16; functions, userdata and cdata become nil.
msgpack.pack(...)#
Packs all arguments one after another into one string.
| Parameter | Type | Description |
|---|---|---|
... |
any |
values |
Returns: string.
local s = msgpack.pack({ 7, 8 }, "x", true)
print(#s, " bytes")
msgpack.unpack(s: string)#
Unpacks all values from a string.
| Parameter | Type | Description |
|---|---|---|
s |
string |
packed data |
Returns: all unpacked values.
local s = msgpack.pack({ 7, 8 }, "x")
local t, x = msgpack.unpack(s)
print(t[2], " ", x) --> 8 x
msgpack.unpack_one(s: string, offset?: number)#
Unpacks one value starting at an offset.
| Parameter | Type | Description |
|---|---|---|
s |
string |
packed data |
offset |
number |
offset in bytes, default 0 |
Returns: number, any — the offset of the next value (-1 if the data is over) and the value itself.
local s = msgpack.pack(1, 2, 3)
local off, v = 0, nil
while off ~= -1 do
off, v = msgpack.unpack_one(s, off)
print(v)
end
msgpack.unpack_limit(s: string, limit: number, offset?: number)#
Unpacks at most limit values.
| Parameter | Type | Description |
|---|---|---|
s |
string |
packed data |
limit |
number |
how many values to unpack |
offset |
number |
offset in bytes, default 0 |
Returns: number, ... — the offset of the next value (-1 if the data is over) and the unpacked values.
local s = msgpack.pack("a", "b", "c", "d")
local off, x, y = msgpack.unpack_limit(s, 2)
print(x, y, " next at ", off) --> ab next at 4