diff --git a/src/pegasus/json.lua b/src/pegasus/json.lua new file mode 100644 index 0000000..1b196dd --- /dev/null +++ b/src/pegasus/json.lua @@ -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, +}