aboutsummaryrefslogtreecommitdiffstats
path: root/util/json.lua
diff options
context:
space:
mode:
authorWaqas Hussain <waqas20@gmail.com>2013-04-23 14:41:52 -0400
committerWaqas Hussain <waqas20@gmail.com>2013-04-23 14:41:52 -0400
commitbc17ecdd0dd77e73eae50bb2eb31d4b7ae358225 (patch)
treea87980637405b12cc785451aca642a5ac738140c /util/json.lua
parent9017588e97e9c431e4d55b217188cbb58011f914 (diff)
downloadprosody-bc17ecdd0dd77e73eae50bb2eb31d4b7ae358225.tar.gz
prosody-bc17ecdd0dd77e73eae50bb2eb31d4b7ae358225.zip
util.json: Make encode(decode("[]"))=="[]".
Diffstat (limited to 'util/json.lua')
-rw-r--r--util/json.lua12
1 files changed, 10 insertions, 2 deletions
diff --git a/util/json.lua b/util/json.lua
index ff7351a7..e8de4d2d 100644
--- a/util/json.lua
+++ b/util/json.lua
@@ -18,6 +18,9 @@ local error = error;
local newproxy, getmetatable = newproxy, getmetatable;
local print = print;
+local has_array, array = pcall(require, "util.array");
+local array_mt = hasarray and getmetatable(array()) or {};
+
--module("json")
local json = {};
@@ -165,7 +168,12 @@ function simplesave(o, buffer)
elseif t == "string" then
stringsave(o, buffer);
elseif t == "table" then
- tablesave(o, buffer);
+ local mt = getmetatable(o);
+ if mt == array_mt then
+ arraysave(o, buffer);
+ else
+ tablesave(o, buffer);
+ end
elseif t == "boolean" then
t_insert(buffer, (o and "true" or "false"));
else
@@ -237,7 +245,7 @@ function json.decode(json)
local readvalue;
local function readarray()
- local t = {};
+ local t = setmetatable({}, array_mt);
next(); -- skip '['
skipstuff();
if ch == "]" then next(); return t; end