Switch from luci-lib-json to luci-lib-jsonc

Apart from replacing a patch for the former by two patches for latter,
this involved minimal adaptations of the lua scripts in the following
packages:

* gluon-announce
* gluon-announced
* gluon-mesh-batman-adv-core
* gluon-status-page
This commit is contained in:
Jan-Philipp Litza 2015-08-30 20:53:44 +02:00
parent 4405f39869
commit 15a80aaffd
17 changed files with 98 additions and 66 deletions

View File

@ -11,7 +11,7 @@ include $(INCLUDE_DIR)/package.mk
define Package/gluon-announce define Package/gluon-announce
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
DEPENDS:=+gluon-core +luci-lib-json +lua-ethtool-stats DEPENDS:=+gluon-core +luci-lib-jsonc +lua-ethtool-stats
TITLE:=Lua scripts announcing various information TITLE:=Lua scripts announcing various information
endef endef

View File

@ -1,10 +1,9 @@
#!/usr/bin/lua #!/usr/bin/lua
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local json = require 'luci.json' local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12' local ltn12 = require 'luci.ltn12'
local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d' local announce_dir = '/lib/gluon/announce/' .. arg[1] .. '.d'
encoder = json.Encoder(announce.collect_dir(announce_dir)) print(json.stringify(announce.collect_dir(announce_dir)))
ltn12.pump.all(encoder:source(), ltn12.sink.file(io.stdout))

View File

@ -3,7 +3,6 @@
module('gluon.announce', package.seeall) module('gluon.announce', package.seeall)
fs = require 'nixio.fs' fs = require 'nixio.fs'
json = require 'luci.json'
uci = require('luci.model.uci').cursor() uci = require('luci.model.uci').cursor()
util = require 'luci.util' util = require 'luci.util'
@ -16,7 +15,7 @@ local function collect_entry(entry)
end end
function collect_dir(dir) function collect_dir(dir)
local ret = { [json.null] = true } local ret = { [{}] = true }
for entry in fs.dir(dir) do for entry in fs.dir(dir) do
if entry:sub(1, 1) ~= '.' then if entry:sub(1, 1) ~= '.' then

View File

@ -1,6 +1,6 @@
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local deflate = require 'deflate' local deflate = require 'deflate'
local json = require 'luci.json' local json = require 'luci.jsonc'
local function collect(type) local function collect(type)
@ -12,7 +12,7 @@ module('gluon.announced', package.seeall)
function handle_request(query) function handle_request(query)
if query:match('^nodeinfo$') then if query:match('^nodeinfo$') then
return json.encode(collect('nodeinfo')) return json.stringify(collect('nodeinfo'))
end end
local m = query:match('^GET ([a-z ]+)$') local m = query:match('^GET ([a-z ]+)$')
@ -27,7 +27,7 @@ function handle_request(query)
end end
if next(data) then if next(data) then
return deflate.compress(json.encode(data)) return deflate.compress(json.stringify(data))
end end
end end
end end

View File

@ -22,7 +22,7 @@ function batadv()
if mac1 ~= nil and mac1 == mac2 then if mac1 ~= nil and mac1 == mac2 then
ifaddress = ifname2address(ifname) ifaddress = ifname2address(ifname)
if interfaces[ifaddress] == nil then if interfaces[ifaddress] == nil then
interfaces[ifaddress] = { neighbours = { [json.null] = true } } interfaces[ifaddress] = { neighbours = { [{}] = true } }
end end
interfaces[ifaddress].neighbours[mac1] = { tq = tonumber(tq) interfaces[ifaddress].neighbours[mac1] = { tq = tonumber(tq)

View File

@ -49,5 +49,5 @@ return {
wireless = nil_table(wireless), wireless = nil_table(wireless),
tunnel = nil_table(tunnel), tunnel = nil_table(tunnel),
other = nil_table(other), other = nil_table(other),
[json.null] = true [{}] = true
} }

View File

@ -1,4 +1,4 @@
local json = require 'luci.json' local json = require 'luci.jsonc'
local ltn12 = require 'luci.ltn12' local ltn12 = require 'luci.ltn12'
local nixio = require 'nixio' local nixio = require 'nixio'
local site = require 'gluon.site_config' local site = require 'gluon.site_config'
@ -11,8 +11,8 @@ if not fastd_sock:connect(socket_path) then
return nil return nil
end end
local decoder = json.Decoder() local decoder = json.new()
ltn12.pump.all(ltn12.source.file(fastd_sock), decoder:sink()) ltn12.pump.all(ltn12.source.file(fastd_sock), function(chunk) decoder:parse(chunk) end)
local status = decoder:get() local status = decoder:get()
@ -27,7 +27,7 @@ local function peer_connection(config)
established = peer.connection.established/1000 established = peer.connection.established/1000
} }
else else
return json.null return function()end -- nil
end end
end end
end end

View File

@ -12,7 +12,7 @@ define Package/gluon-status-page
SECTION:=gluon SECTION:=gluon
CATEGORY:=Gluon CATEGORY:=Gluon
TITLE:=Adds a status page showing information about the node. TITLE:=Adds a status page showing information about the node.
DEPENDS:=+gluon-core +uhttpd +gluon-neighbour-info +gluon-announce +libiwinfo-lua DEPENDS:=+gluon-core +uhttpd +gluon-neighbour-info +gluon-announce +libiwinfo-lua +luci-lib-jsonc
endef endef
define Package/gluon-status-page/description define Package/gluon-status-page/description

View File

@ -1,6 +1,6 @@
#!/usr/bin/lua #!/usr/bin/lua
local json = require 'luci.json' local json = require 'luci.jsonc'
local nixio = require 'nixio' local nixio = require 'nixio'
function neighbours() function neighbours()
@ -25,7 +25,7 @@ io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: text/event-stream\n\n") io.write("Content-type: text/event-stream\n\n")
while true do while true do
local neighbours = json.encode(neighbours()) local neighbours = json.stringify(neighbours())
io.write("data: " .. neighbours .. "\n\n") io.write("data: " .. neighbours .. "\n\n")
io.flush() io.flush()
nixio.nanosleep(1, 0) nixio.nanosleep(1, 0)

View File

@ -1,7 +1,7 @@
#!/usr/bin/lua #!/usr/bin/lua
util = require 'luci.util' util = require 'luci.util'
json = require 'luci.json' json = require 'luci.jsonc'
nixio = require 'nixio' nixio = require 'nixio'
iwinfo = require 'iwinfo' iwinfo = require 'iwinfo'
@ -45,7 +45,7 @@ io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: text/event-stream\n\n") io.write("Content-type: text/event-stream\n\n")
while true do while true do
local stations = json.encode(get_stations(iw, ifname)) local stations = json.stringify(get_stations(iw, ifname))
io.write("data: " .. stations .. "\n\n") io.write("data: " .. stations .. "\n\n")
io.flush() io.flush()
nixio.nanosleep(0, 150e6) nixio.nanosleep(0, 150e6)

View File

@ -1,7 +1,7 @@
#!/usr/bin/lua #!/usr/bin/lua
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local json = require 'luci.json' local json = require 'luci.jsonc'
local util = require 'luci.util' local util = require 'luci.util'
local nixio = require 'nixio' local nixio = require 'nixio'
@ -11,7 +11,7 @@ io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: text/event-stream\n\n") io.write("Content-type: text/event-stream\n\n")
while true do while true do
local data = json.encode(announce.collect_dir(announce_dir)) local data = json.stringify(announce.collect_dir(announce_dir))
io.write("data: " .. data .. "\n\n") io.write("data: " .. data .. "\n\n")
io.flush() io.flush()
nixio.nanosleep(1, 0) nixio.nanosleep(1, 0)

View File

@ -1,7 +1,7 @@
#!/usr/bin/lua #!/usr/bin/lua
util = require 'luci.util' util = require 'luci.util'
json = require 'luci.json' json = require 'luci.jsonc'
fs = require 'nixio.fs' fs = require 'nixio.fs'
io.write("Access-Control-Allow-Origin: *\n") io.write("Access-Control-Allow-Origin: *\n")
@ -21,4 +21,4 @@ for _, line in ipairs(util.split(list)) do
end end
end end
io.write(json.encode(interfaces)) io.write(json.stringify(interfaces))

View File

@ -1,7 +1,7 @@
#!/usr/bin/lua #!/usr/bin/lua
local announce = require 'gluon.announce' local announce = require 'gluon.announce'
local json = require 'luci.json' local json = require 'luci.jsonc'
local util = require 'luci.util' local util = require 'luci.util'
local nixio = require 'nixio' local nixio = require 'nixio'
@ -10,6 +10,6 @@ local announce_dir = '/lib/gluon/announce/nodeinfo.d/'
io.write("Access-Control-Allow-Origin: *\n") io.write("Access-Control-Allow-Origin: *\n")
io.write("Content-type: application/json\n\n") io.write("Content-type: application/json\n\n")
local data = json.encode(announce.collect_dir(announce_dir)) local data = json.stringify(announce.collect_dir(announce_dir))
io.write(data) io.write(data)
io.flush() io.flush()

View File

@ -4,7 +4,7 @@ local util = require("luci.util")
local fs = require("nixio.fs") local fs = require("nixio.fs")
local ltn12 = require 'luci.ltn12' local ltn12 = require 'luci.ltn12'
local sys = require("luci.sys") local sys = require("luci.sys")
local json = require("luci.json") local json = require("luci.jsonc")
local nixio = require 'nixio' local nixio = require 'nixio'
local platform_info = require("platform_info") local platform_info = require("platform_info")
@ -20,7 +20,7 @@ function neighbours(ifname)
local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 3 -i " .. ifname) local info = util.exec("gluon-neighbour-info -d ff02::2:1001 -p 1001 -r nodeinfo -t 3 -i " .. ifname)
local macs = {} local macs = {}
for _, line in ipairs(util.split(info)) do for _, line in ipairs(util.split(info)) do
local data = json.decode(line) local data = json.parse(line)
if data then if data then
local function add_macs(list) local function add_macs(list)
if list then if list then
@ -93,8 +93,8 @@ local stat, fastd_status = pcall(
local fastd_sock = nixio.socket('unix', 'stream') local fastd_sock = nixio.socket('unix', 'stream')
assert(fastd_sock:connect('/var/run/fastd.mesh_vpn.socket')) assert(fastd_sock:connect('/var/run/fastd.mesh_vpn.socket'))
decoder = json.Decoder() decoder = json.new()
ltn12.pump.all(ltn12.source.file(fastd_sock), decoder:sink()) ltn12.pump.all(ltn12.source.file(fastd_sock), function(chunk) decoder:parse(chunk) end)
return decoder:get() return decoder:get()
end end
) )

View File

@ -1,36 +0,0 @@
From: Matthias Schiffer <mschiffer@universe-factory.net>
Date: Thu, 4 Jun 2015 21:03:24 +0200
Subject: luci-lib-json: ignore null keys to allow encoding empty objects
There is currently no way to encode an empty object {}, as empty tables are
encoded as empty lists [].
With this patch, encode() will ignore table fields with the key json.null (which
doesn't make sense anyways). This allows adding a field with key json.null to
force encoding it as an object.
Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
diff --git a/libs/luci-lib-json/luasrc/json.lua b/libs/luci-lib-json/luasrc/json.lua
index 416b25f..f7b57f9 100644
--- a/libs/luci-lib-json/luasrc/json.lua
+++ b/libs/luci-lib-json/luasrc/json.lua
@@ -149,11 +149,13 @@ function Encoder.parse_iter(self, obj)
local first = true
for key, entry in pairs(obj) do
- first = first or self:put(",")
- first = first and false
- self:parse_string(tostring(key))
- self:put(":")
- self:dispatch(entry)
+ if key ~= null then
+ first = first or self:put(",")
+ first = first and false
+ self:parse_string(tostring(key))
+ self:put(":")
+ self:dispatch(entry)
+ end
end
self:put("}")

View File

@ -0,0 +1,32 @@
From: Jan-Philipp Litza <janphilipp@litza.de>
Date: Sun, 30 Aug 2015 15:42:52 +0200
Subject: luci-lib-jsonc: Ignore non-string-or-number keys in tables
Previously, the following caused a segmentation fault:
json.stringify({[{}] = true})
This was caused by lua_tostring() returning NULL for anything but
strings and numbers, letting json_object_object_add crash.
This patch makes jsonc ignore all keys which have no string
representation altogether.
Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
diff --git a/libs/luci-lib-jsonc/src/jsonc.c b/libs/luci-lib-jsonc/src/jsonc.c
index 49cb21f..827fde8 100644
--- a/libs/luci-lib-jsonc/src/jsonc.c
+++ b/libs/luci-lib-jsonc/src/jsonc.c
@@ -286,8 +286,9 @@ static struct json_object * _lua_to_json(lua_State *L, int index)
lua_pushvalue(L, -2);
key = lua_tostring(L, -1);
- json_object_object_add(obj, key,
- _lua_to_json(L, lua_gettop(L) - 1));
+ if (key)
+ json_object_object_add(obj, key,
+ _lua_to_json(L, lua_gettop(L) - 1));
lua_pop(L, 2);
}

View File

@ -0,0 +1,38 @@
From: Jan-Philipp Litza <janphilipp@litza.de>
Date: Sun, 30 Aug 2015 15:45:49 +0200
Subject: luci-lib-jsonc: allow encoding empty lists
To be consistent with the behavior of luci-lib-json, an empty Lua table
should be encoded to an empty JSON list, not an empty JSON object.
To still allow encoding empty JSON objects, the usage of anything other
than a number or a string as a key (for example an empty table or a
function) can be used to force encoding as an object:
json.stringify({}) -- "[]"
json.stringify({[{}] = true}) -- "{}"
Signed-off-by: Jan-Philipp Litza <janphilipp@litza.de>
diff --git a/libs/luci-lib-jsonc/src/jsonc.c b/libs/luci-lib-jsonc/src/jsonc.c
index 827fde8..971fb12 100644
--- a/libs/luci-lib-jsonc/src/jsonc.c
+++ b/libs/luci-lib-jsonc/src/jsonc.c
@@ -222,7 +222,7 @@ static int _lua_test_array(lua_State *L, int index)
out:
lua_pop(L, 2);
- return 0;
+ return -1;
}
/* check for holes */
@@ -254,7 +254,7 @@ static struct json_object * _lua_to_json(lua_State *L, int index)
case LUA_TTABLE:
max = _lua_test_array(L, index);
- if (max > 0)
+ if (max >= 0)
{
obj = json_object_new_array();