summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2015-06-04 21:03:24 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2015-06-04 21:13:41 +0200
commit304eec2ebaf983fd067e5ae719f8ec023aad8b30 (patch)
tree32a1aa489cc7414d135070691141bd8e3faf097d
parenta835fc013b1420666e2a437b963ef7c5f07aea69 (diff)
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>
-rw-r--r--libs/luci-lib-json/luasrc/json.lua12
1 files changed, 7 insertions, 5 deletions
diff --git a/libs/luci-lib-json/luasrc/json.lua b/libs/luci-lib-json/luasrc/json.lua
index 416b25faa5..f7b57f916c 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("}")