diff options
author | Peter Kozak <spag@golwen.net> | 2013-04-09 13:25:52 +0200 |
---|---|---|
committer | Peter Kozak <spag@golwen.net> | 2013-04-09 13:25:52 +0200 |
commit | 58741154e0147ee878b093fc25051ef9d1b93e4a (patch) | |
tree | 7e27a6c4f9e3b0087bd79692d10f8415b8042474 /misc/freeswitch/scripts | |
parent | d6adfbba723de9732f24efd71f80c422ae1a40f0 (diff) |
max_depth added
Diffstat (limited to 'misc/freeswitch/scripts')
-rw-r--r-- | misc/freeswitch/scripts/common/array.lua | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/misc/freeswitch/scripts/common/array.lua b/misc/freeswitch/scripts/common/array.lua index b1b7a71..c3cabec 100644 --- a/misc/freeswitch/scripts/common/array.lua +++ b/misc/freeswitch/scripts/common/array.lua @@ -4,6 +4,8 @@ module(...,package.seeall) +MAX_JSON_DEPTH = 100; + function try(array, arguments) if type(arguments) ~= 'string' or type(array) ~= 'table' then return nil; @@ -79,12 +81,19 @@ function keys_to_s(array, separator, prefix, suffix) end -- convert to JSON -function to_json(array) +function to_json(array, max_depth) + max_depth = tonumber(max_depth) or MAX_JSON_DEPTH; + max_depth = max_depth - 1; + + if max_depth <= 0 then + return 'null'; + end + require 'common.str'; local buffer = '{'; for key, value in pairs(array) do if type(value) == 'table' then - buffer = buffer .. '"' .. key .. '":' .. to_json(value) .. ','; + buffer = buffer .. '"' .. key .. '":' .. to_json(value, max_depth) .. ','; else buffer = buffer .. '"' .. key .. '":' .. common.str.to_json(value) .. ','; end |