aboutsummaryrefslogtreecommitdiffstats
path: root/util/json.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2012-01-22 22:55:49 +0000
committerMatthew Wild <mwild1@gmail.com>2012-01-22 22:55:49 +0000
commit4d7ce5ef98953b67c80cc7f2fc347f01ad9cdd4e (patch)
treeacc1b9c5576f0ba21b56fd5b133a4a173798d042 /util/json.lua
parentb803655fc3922d51ccfb44eebe8540e3ba562b2b (diff)
parent075f0cbd7c732c108e03ec8596e9934db7b8b06a (diff)
downloadprosody-4d7ce5ef98953b67c80cc7f2fc347f01ad9cdd4e.tar.gz
prosody-4d7ce5ef98953b67c80cc7f2fc347f01ad9cdd4e.zip
Merge with trunk
Diffstat (limited to 'util/json.lua')
-rw-r--r--util/json.lua31
1 files changed, 25 insertions, 6 deletions
diff --git a/util/json.lua b/util/json.lua
index 5d0b0876..efc602f0 100644
--- a/util/json.lua
+++ b/util/json.lua
@@ -1,6 +1,6 @@
local type = type;
-local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove;
+local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort;
local s_char = string.char;
local tostring, tonumber = tostring, tonumber;
local pairs, ipairs = pairs, ipairs;
@@ -79,11 +79,25 @@ function tablesave(o, buffer)
if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then
t_insert(buffer, "{");
local mark = #buffer;
- for k,v in pairs(hash) do
- stringsave(k, buffer);
- t_insert(buffer, ":");
- simplesave(v, buffer);
- t_insert(buffer, ",");
+ if buffer.ordered then
+ local keys = {};
+ for k in pairs(hash) do
+ t_insert(keys, k);
+ end
+ t_sort(keys);
+ for _,k in ipairs(keys) do
+ stringsave(k, buffer);
+ t_insert(buffer, ":");
+ simplesave(hash[k], buffer);
+ t_insert(buffer, ",");
+ end
+ else
+ for k,v in pairs(hash) do
+ stringsave(k, buffer);
+ t_insert(buffer, ":");
+ simplesave(v, buffer);
+ t_insert(buffer, ",");
+ end
end
if next(__hash) ~= nil then
t_insert(buffer, "\"__hash\":[");
@@ -129,6 +143,11 @@ function json.encode(obj)
simplesave(obj, t);
return t_concat(t);
end
+function json.encode_ordered(obj)
+ local t = { ordered = true };
+ simplesave(obj, t);
+ return t_concat(t);
+end
-----------------------------------