diff --git a/rockspecs/pegasus-dev-1.rockspec b/rockspecs/pegasus-dev-1.rockspec index 9aa32b0..9535360 100644 --- a/rockspecs/pegasus-dev-1.rockspec +++ b/rockspecs/pegasus-dev-1.rockspec @@ -34,6 +34,7 @@ build = { modules = { ['pegasus.init'] = "src/pegasus/init.lua", ['pegasus.handler'] = 'src/pegasus/handler.lua', + ['pegasus.json'] = 'src/pegasus/json.lua', ['pegasus.request'] = 'src/pegasus/request.lua', ['pegasus.response'] = 'src/pegasus/response.lua', ['pegasus.compress'] = 'src/pegasus/compress.lua', 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, +}