diff options
author | Matthew Wild <mwild1@gmail.com> | 2018-09-21 14:35:35 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2018-09-21 14:35:35 +0100 |
commit | d338d4b82ca2098c365f3fa7a4c568e222fe9b7a (patch) | |
tree | f838b4314f36c29d318fee6352c7460e3ba80c6d | |
parent | 431996d9448d0038e8a7dbf86a0d7f87994ad00e (diff) | |
download | prosody-d338d4b82ca2098c365f3fa7a4c568e222fe9b7a.tar.gz prosody-d338d4b82ca2098c365f3fa7a4c568e222fe9b7a.zip |
util.json: Use util.iterators.sorted_pairs() in ordered mode
-rw-r--r-- | util/json.lua | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/util/json.lua b/util/json.lua index 05af709a..a750da2e 100644 --- a/util/json.lua +++ b/util/json.lua @@ -7,10 +7,10 @@ -- local type = type; -local t_insert, t_concat, t_remove, t_sort = table.insert, table.concat, table.remove, table.sort; +local t_insert, t_concat, t_remove = table.insert, table.concat, table.remove; local s_char = string.char; local tostring, tonumber = tostring, tonumber; -local pairs, ipairs = pairs, ipairs; +local pairs, ipairs, spairs = pairs, ipairs, require "util.iterators".sorted_pairs; local next = next; local getmetatable, setmetatable = getmetatable, setmetatable; local print = print; @@ -95,25 +95,12 @@ function tablesave(o, buffer) if next(__hash) ~= nil or next(hash) ~= nil or next(__array) == nil then t_insert(buffer, "{"); local mark = #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 + local _pairs = buffer.ordered and spairs or pairs; + for k,v in _pairs(hash) do + stringsave(k, buffer); + t_insert(buffer, ":"); + simplesave(v, buffer); + t_insert(buffer, ","); end if next(__hash) ~= nil then t_insert(buffer, "\"__hash\":["); |