Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

MessagePack for LuaJIT (using FFI, no bindings)

branch: master

fix table classifier

add a possibly slightly faster algorithm
as an alternative (not by default because it
accesses keys)
latest commit a7f16a0826
Pierre Chapuis authored March 08, 2014
Octocat-spinner-32 tests fix table classifier March 08, 2014
Octocat-spinner-32 LICENSE.txt copyright bump January 19, 2013
Octocat-spinner-32 Makefile Plain Lua support. Fixes #3. October 10, 2012
Octocat-spinner-32 README.md fix table classifier March 08, 2014
Octocat-spinner-32 luajit-msgpack-pure.lua fix table classifier March 08, 2014
README.md

luajit-msgpack-pure

Presentation

This is yet another implementation of MessagePack for LuaJIT. However, unlike luajit-msgpack, luajit-msgpack-pure does not depend on the MessagePack C library. Everything is re-implemented in LuaJIT code (using the FFI but only to manipulate data structures).

Alternatives

If I had to pick a single MessagePack implementation, it would be lua-MessagePack. It is pure Lua, its performance is very close to luajit-msgpack-pure and it supports the latest revision of the standard. If it had existed earlier, I would not have written this one. If you start a new project, use it.

Another interesting implementation is lua-cmsgpack, written in C specifically for use in Redis.

Other implementations:

Before luajit-msgpack-pure, I had written luajit-msgpack, a FFI binding which is now deprecated.

TODO

  • Missing datatype tests
  • Comparison tests vs. other implementations

Usage

Basics

local mp = require "luajit-msgpack-pure"
local my_data = {this = {"is",4,"test"}}
local encoded = mp.pack(my_data)
local offset,decoded = mp.unpack(encoded)
assert(offset == #encoded)

Concatenating encoded data

local mp = require "luajit-msgpack-pure"
local my_data_1 = 42
local my_data_2 = "foo"
local encoded = mp.pack(my_data_1) .. mp.pack(my_data_2)
local offset_1,decoded_1 = mp.unpack(encoded)
assert(decoded_1 == 42)
local offset_2,decoded_2 = mp.unpack(encoded,offset_1)
assert(decoded_2 == "foo")
local offset_3,decoded_3 = mp.unpack(encoded,offset_2)
assert((not offset_3) and (decoded_3 == nil))

Setting floating point precision

local mp = require "luajit-msgpack-pure"
local my_data = math.pi
local encoded_1 = mp.pack(my_data) -- default is double
local offset_1,decoded_1 = mp.unpack(encoded_1)
assert(offset_1 == 9) -- 1 byte overhead + 8 bytes double
assert(decoded_1 == math.pi)
mp.set_fp_type("float")
local encoded_2 = mp.pack(my_data)
local offset_2,decoded_2 = mp.unpack(encoded_2)
assert(offset_2 == 5) -- 1 byte overhead + 5 bytes float
assert(decoded_2 ~= math.pi) -- loss of precision
mp.set_fp_type("double") -- back to double precision
local encoded_3 = mp.pack(my_data)
local offset_3,decoded_3 = mp.unpack(encoded_3)
assert((offset_3 == 9) and (decoded_3 == math.pi))

Copyright

Copyright (c) 2011-2014 Pierre Chapuis

Something went wrong with that request. Please try again.