luci-base: use serialize_json() in luci.http
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 18 Jan 2015 18:01:04 +0000 (19:01 +0100)
committerJo-Philipp Wich <jow@openwrt.org>
Thu, 22 Jan 2015 14:01:50 +0000 (15:01 +0100)
Signed-off-by: Jo-Philipp Wich <jow@openwrt.org>
modules/luci-base/luasrc/http.lua

index a5329e51d6835a222ce5e8c1f1046b6b6af555ef..5e1d73e3a0969509eb31d8d2d69b3cf8472784a5 100644 (file)
@@ -285,40 +285,5 @@ 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
-               if (x ~= x) then
-                       -- NaN is the only value that doesn't equal to itself.
-                       write("Number.NaN")
-               else
-                       write(tostring(x))
-               end
-       else
-               write('"%s"' % tostring(x):gsub('["%z\1-\31]', function(c)
-                       return '\\u%04x' % c:byte(1)
-               end))
-       end
+       util.serialize_json(x, write)
 end