-urldecode = luci.http.protocol.urldecode
-urlencode = luci.http.protocol.urlencode
---[[
-function urldecode(str)
- str = str:gsub("+", " ")
- str = str:gsub("%%(%x%x)",
- function(h) return string.char(tonumber(h,16)) end)
- str = str:gsub("\r\n", "\n")
- return str
-end
-
-function urlencode(str)
- str = str:gsub("\n", "\r\n")
- str = str:gsub("([^%w ])",
- function (c) return string.format ("%%%02X", string.byte(c)) end)
- str = str:gsub(" ", "+")
- return str
-end
-]]--
\ No newline at end of file
+--- Return the URL-decoded equivalent of a string.
+-- @param str URL-encoded string
+-- @param no_plus Don't decode + to " "
+-- @return URL-decoded string
+-- @see urlencode
+urldecode = protocol.urldecode
+
+--- Return the URL-encoded equivalent of a string.
+-- @param str Source string
+-- @return URL-encoded string
+-- @see urldecode
+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