libs/web: add luci.http.write_json()
This commit is contained in:
parent
fcd9579fd4
commit
561673c0a1
1 changed files with 61 additions and 21 deletions
|
@ -29,8 +29,10 @@ local protocol = require "luci.http.protocol"
|
||||||
local util = require "luci.util"
|
local util = require "luci.util"
|
||||||
local string = require "string"
|
local string = require "string"
|
||||||
local coroutine = require "coroutine"
|
local coroutine = require "coroutine"
|
||||||
|
local table = require "table"
|
||||||
|
|
||||||
local pairs, tostring, error = pairs, tostring, error
|
local ipairs, pairs, next, type, tostring, error =
|
||||||
|
ipairs, pairs, next, type, tostring, error
|
||||||
|
|
||||||
--- LuCI Web Framework high-level HTTP functions.
|
--- LuCI Web Framework high-level HTTP functions.
|
||||||
module "luci.http"
|
module "luci.http"
|
||||||
|
@ -276,14 +278,18 @@ end
|
||||||
--- Create a querystring out of a table of key - value pairs.
|
--- Create a querystring out of a table of key - value pairs.
|
||||||
-- @param table Query string source table
|
-- @param table Query string source table
|
||||||
-- @return Encoded HTTP query string
|
-- @return Encoded HTTP query string
|
||||||
function build_querystring(table)
|
function build_querystring(q)
|
||||||
local s="?"
|
local s = { "?" }
|
||||||
|
|
||||||
for k, v in pairs(table) do
|
for k, v in pairs(q) do
|
||||||
s = s .. urlencode(k) .. "=" .. urlencode(v) .. "&"
|
if #s > 1 then s[#s+1] = "&" end
|
||||||
|
|
||||||
|
s[#s+1] = urldecode(k)
|
||||||
|
s[#s+1] = "="
|
||||||
|
s[#s+1] = urldecode(v)
|
||||||
end
|
end
|
||||||
|
|
||||||
return s
|
return table.concat(s, "")
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Return the URL-decoded equivalent of a string.
|
--- Return the URL-decoded equivalent of a string.
|
||||||
|
@ -298,3 +304,37 @@ urldecode = protocol.urldecode
|
||||||
-- @return URL-encoded string
|
-- @return URL-encoded string
|
||||||
-- @see urldecode
|
-- @see urldecode
|
||||||
urlencode = protocol.urlencode
|
urlencode = protocol.urlencode
|
||||||
|
|
||||||
|
--- Send the given data as JSON encoded string.
|
||||||
|
-- @param data Data to send
|
||||||
|
function write_json(x)
|
||||||
|
if x == nil then
|
||||||
|
write("null")
|
||||||
|
elseif type(x) == "table" then
|
||||||
|
local k, v
|
||||||
|
if type(next(x)) == "number" then
|
||||||
|
write("[ ")
|
||||||
|
for k, v in ipairs(x) do
|
||||||
|
write_json(v)
|
||||||
|
if next(x, k) then
|
||||||
|
write(", ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
write(" ]")
|
||||||
|
else
|
||||||
|
write("{ ")
|
||||||
|
for k, v in pairs(x) do
|
||||||
|
write("%q: " % k)
|
||||||
|
write_json(v)
|
||||||
|
if next(x, k) then
|
||||||
|
write(", ")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
write(" }")
|
||||||
|
end
|
||||||
|
elseif type(x) == "number" or type(x) == "boolean" then
|
||||||
|
write(tostring(x))
|
||||||
|
elseif type(x) == "string" then
|
||||||
|
write("%q" % tostring(x))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue