MessagePack库

此库来自这里.

MessagePack API

导入

local msgpack = require "msgpack"

msgpack.encode(table)

将table序列化为msgpack二进制数据.

msgpack.decode(msgpack_string)

将msgpack二进制数据反序列化为lua table.

示例

参考script/test_msgpack.lua

  1. local Log = require ("logging"):new()
  2. local msgpack = require "msgpack"
  3. local msg = msgpack.encode({1, 2, 3, 4, name = "CandyMi"})
  4. Log:DEBUG("序列化完成:"..msg)
  5. Log:DEBUG(msgpack.decode(msg))

输出结果:

  1. [candy@MacBookPro:~/Documents/core_framework] $ ./cfadmin
  2. [2019-07-17 11:36:19,911] [@script/main.lua:6] [DEBUG] : 序列化完成:��nameCandyMi
  3. [2019-07-17 11:36:19,911] [@script/main.lua:8] [DEBUG] : {[1]=1, [2]=2, [3]=3, [4]=4, ["name"]="CandyMi"}

注意

  • int64大整数的精度损失.

  • 不支持稀疏数组与null填充字段.

  • 空表({})默认转换为数组类型.