-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a wrapper around any of the following Lua modules; 'lua-cjson', 'dkjson', or 'rapidjson'.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
-- Wrapper library that supports multiple JSON libraries | ||
local found, jsonlib | ||
|
||
found, jsonlib = pcall(require, "cjson.safe") -- lua-cjson | ||
if found then | ||
return setmetatable({ | ||
-- decode is available | ||
-- encode is available | ||
-- null is available | ||
available = true, | ||
makeArray= function(t) | ||
assert(type(t) == "table", "expected a table") | ||
return setmetatable(t, jsonlib.array_mt) | ||
end, | ||
}, { __index = jsonlib }) | ||
end | ||
|
||
|
||
found, jsonlib = pcall(require, "dkjson") -- dkjson | ||
if found then | ||
local array_mt = { __jsontype='array'} | ||
return setmetatable({ | ||
-- decode is available | ||
-- encode is available | ||
-- null is available | ||
available = true, | ||
makeArray= function(t) | ||
assert(type(t) == "table", "expected a table") | ||
return setmetatable(t, array_mt) | ||
end, | ||
}, { __index = jsonlib }) | ||
end | ||
|
||
|
||
found, jsonlib = pcall(require, "rapidjson") -- rapidjson | ||
if found then | ||
local array_mt = { __jsontype='array'} | ||
return setmetatable({ | ||
decode = function(value) | ||
return jsonlib.decode(value, 1, jsonlib.null) | ||
end, | ||
-- encode is available | ||
-- null is available | ||
available = true, | ||
makeArray= function(t) | ||
assert(type(t) == "table", "expected a table") | ||
return setmetatable(t, array_mt) | ||
end, | ||
}, { __index = jsonlib }) | ||
end | ||
|
||
|
||
local err = function() | ||
return nil, "no json library available, install 'lua-cjson', 'dkjson', or 'rapidjson'" | ||
end | ||
|
||
return { | ||
available = false, | ||
encode = err, | ||
decode = err, | ||
makeArray = err, | ||
} |