diff --git a/lib/lor/lib/request.lua b/lib/lor/lib/request.lua index a561cc3..782c756 100755 --- a/lib/lor/lib/request.lua +++ b/lib/lor/lib/request.lua @@ -22,6 +22,10 @@ function Request:new() body[k] = v end end + elseif sfind(header, "application/ljpack", 1, true) then + ngx.req.read_body() + local sb_str = ngx.req.get_body_data() + body = utils.ljpack_decode(sb_str) elseif sfind(header, "application/json", 1, true) then ngx.req.read_body() local json_str = ngx.req.get_body_data() diff --git a/lib/lor/lib/response.lua b/lib/lor/lib/response.lua index c69fac7..dd3f5b5 100755 --- a/lib/lor/lib/response.lua +++ b/lib/lor/lib/response.lua @@ -47,6 +47,19 @@ function Response:json(data, empty_table_as_object) self:_send(utils.json_encode(data, empty_table_as_object)) end + +function Response:json_stably(data) + self:set_header('Content-Type', 'application/json; charset=utf-8') + self:_send(utils.json_stably_encode(data)) +end + + + +function Response:ljpack(data) + self:set_header('Content-Type', 'application/ljpack') + self:__send(utils.ljpack_encode(data)) +end + function Response:redirect(url, code, query) if url and not code and not query then -- only one param ngx.redirect(url) @@ -111,6 +124,11 @@ function Response:_send(content) ngx.say(content) end +function Response:__send(content) + ngx.status = self.http_status or 200 + ngx.print(content) +end + function Response:get_body() return self.body end diff --git a/lib/lor/lib/utils/utils.lua b/lib/lor/lib/utils/utils.lua index 52ca1d5..d0d3f23 100755 --- a/lib/lor/lib/utils/utils.lua +++ b/lib/lor/lib/utils/utils.lua @@ -8,6 +8,8 @@ local sgsub = string.gsub local smatch = string.match local table_insert = table.insert local json = require("cjson") +local dkjson = require("dkjson") +local st = require("string.buffer") local _M = {} @@ -69,7 +71,7 @@ function _M.json_encode(data, empty_table_as_object) local json_value if json.encode_empty_table_as_object then -- empty table encoded as array default - json.encode_empty_table_as_object(empty_table_as_object or false) + json.encode_empty_table_as_object(empty_table_as_object or false) end if require("ffi").os ~= "Windows" then json.encode_sparse_array(true) @@ -78,6 +80,12 @@ function _M.json_encode(data, empty_table_as_object) return json_value end +function _M.json_stably_encode(data) + local json_value + pcall(function(d) json_value = dkjson.encode(d) end, data) + return json_value +end + function _M.json_decode(str) local ok, data = pcall(json.decode, str) if ok then @@ -85,6 +93,19 @@ function _M.json_decode(str) end end +function _M.ljpack_encode(data) + local st_value + pcall(function(d) st_value = st.encode(d) end, data) + return st_value +end + +function _M.ljpack_decode(str) + local ok, data = pcall(st.decode, str) + if ok then + return data + end +end + function _M.start_with(str, substr) if str == nil or substr == nil then return false