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.
lua-devel
(linux) brew install lua luarocks
cmake
>= 3.1.0
, cmake 2.8 may work but not well tested.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()
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>
cmake --build build --config Release
to build the rapidjosn.so
or rapidjosn.dll
library.
Then link that library to you project or copy to desired place.
Tips: use
cmake --help
to see a list of generator-name available.
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 |
Clone or download source code, in the project root folder:
luarocks install luautf8
luarocks install busted
luarocks make
busted
To compare speed of rapidjson and other json libraries:
lua performance/run.lua
The result on my Macbook Pro shows:
See API reference.
luarocks upload rapidjson-*.*.*-1.rockspec
rapidjson.null
type to lightuserdata and fixes the issue when it accessed by different Lua States.pushDecoded
.RAPIDJSON_INCLUDE_DIRS
to specified RapidJSON include directory.__jsontype
set to 'array'
.__jsontype
and length > 0.rapidjson
.option.sort_keys
option to rapidjson.encode()
and rapidjson.dump()
, and default value for sort_keys
is false
.rapidjson._NAME
("rapidjson"
) and rapidjson._VERSION
.rapidjson.object()
and rapidjson.array()
just set metatable field __jsontype
to 'object'
and 'array'
if passed table already have a metatable.false
rather than nil
.