luci-lib-json: ignore null keys to allow encoding empty objects 402/head
authorMatthias Schiffer <mschiffer@universe-factory.net>
Thu, 4 Jun 2015 19:03:24 +0000 (21:03 +0200)
committerMatthias Schiffer <mschiffer@universe-factory.net>
Thu, 4 Jun 2015 19:13:41 +0000 (21:13 +0200)
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>
libs/luci-lib-json/luasrc/json.lua

index 416b25faa5074c17ef233b3db39b9c845096db02..f7b57f916c1e7c7d62a30dd62a1fa5e52ee4c7b5 100644 (file)
@@ -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("}")