xpol / lua-rapidjson

A JSON module for Lua based on the very fast RapidJSON library.
MIT License
279 stars 65 forks source link
lua rapidjson

RapidJSON bindings for Lua

License Badge

A json module for LuaJIT 2.0/2.1 and Lua 5.1/5.2/5.3, based on the very fast RapidJSON C++ library.

See project homepage for more information, bug report and feature request.

Dependencies

Usage

luarocks install rapidjson

or if you like to use your own version of RapidJSON, use:

luarocks install rapidjson RAPIDJSON_INCLUDE_DIRS=path/to/rapidjson/include/dir
local rapidjson = require('rapidjson')

rapidjson.encode()
rapidjson.decode()

rapidjson.load()
rapidjson.dump()

Usage without luarocks

  1. Use cmake -H. -Bbuild -G<generator-name> go generate project.

    If you use a non standard lua install location, add environment variable LUA_DIR to the directory contains include and lib for you lua installation. eg.

    LUA_DIR=/usr/local/openresty/luajit cmake -H. -Bbuild -G<generator-name>
  2. cmake --build build --config Release to build the rapidjosn.so or rapidjosn.dll library.

  3. Then link that library to you project or copy to desired place.

Tips: use cmake --help to see a list of generator-name available.

Value Type Mappings

Lua Type JSON type Notes
rapidjson.null null
true true
false false
string string
table array when meta field __jsontype is 'array' or no __jsontype meta filed and table length > 0 or table length == 0 and empty_table_as_array option is specified
table object when not an array, all non string keys and its values are ignored.
number number

Test

Clone or download source code, in the project root folder:

luarocks install luautf8
luarocks install busted
luarocks make
busted

Performance

To compare speed of rapidjson and other json libraries:

lua performance/run.lua

The result on my Macbook Pro shows:

API

See API reference.

Release Steps

  1. Pass all unit tests.
  2. Update version in rapidjson-..*-1.rockspec and update the name of the rockspec file.
  3. Tag source code with that version (v..*), and push.
  4. luarocks upload rapidjson-*.*.*-1.rockspec

Changelog

0.7.0

0.6.1

0.6.0

0.5.2

0.5.1

0.5.0

0.4.5

0.4.4

0.4.3

0.4.2

0.4.1

0.4.0

0.3.0

0.2.0

0.1.0