LuaFFIJson is a Fast JSON parser library written in LUA FFI. It uses LUA FFI to make JSON decoding very fast.
It requires LUA JIT and it's also tested with OpenResty.
JSON decode function is very fast compared to existing LUA/C based JSON decoders.
JSON encode function is comparatively slow because it is written in pure LUA.
It has no other external dependencies.
It is single file library(JsonParser.lua).
It supports UTF-8 and Unicode.
Sample code:
local json = require("JsonParser") local json_decode = json.decode local body = [[{ "en":"I can eat glass and it doesn't hurt me.", "zh-Hant":"我能吞下玻璃而不傷身體。", "zh-Hans":"我能吞下玻璃而不伤身体。", "ja":"私はガラスを食べられます。それは私を傷つけません。", "ko":"나는 유리를 먹을 수 있어요. 그래도 아프지 않아요" }]] local j = json_decode(body) print(j)
Output:
{ en = "I can eat glass and it doesn't hurt me.", ja = "私はガラスを食べられます。それは私を傷つけません。", ko = "나는 유리를 먹을 수 있어요. 그래도 아프지 않아요", ["zh-Hans"] = "我能吞下玻璃而不伤身体。", ["zh-Hant"] = "我能吞下玻璃而不傷身體。" }
Code:
print(j["ko"])
Output:
나는 유리를 먹을 수 있어요. 그래도 아프지 않아요
Code:
local json_encode = json.encode local s = json.encode(j) print(s)
Output:
{"en":"I can eat glass and it doesn't hurt me.","zh-Hans":"我能吞下玻璃而不伤身体。","ko":"나는 유리를 먹을 수 있어요. 그래도 아프지 않아요","zh-Hant":"我能吞下玻璃而不傷身體。","ja":"私はガラスを食べられます。それは私を傷つけません。"}
Benchmark (decode):
JsonParser | cjson | rapidjson | |
int test | 50ms | 91ms | 55ms |
float test | 64ms | 212ms | 59ms |
bool test | 38ms | 69ms | 30ms |
utf8 test | 22ms | 14ms | 15ms |
mixed small test | 48ms | 60ms | 47ms |
mixed medium test | 133ms | 143ms | 134ms |
large para test | 1260ms | 823ms | 206ms |
Test condition:
10000 iteration for each input type with code warmup on my VM machine (2 core) on Mac OS with openresty/1.13.6.1