0004-luci-lib-json-ignore-null-keys-to-allow-encoding-empty-objects.patch 1.2 KB

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