diff options
author | spag <spag@golwen.net> | 2013-03-13 08:35:28 +0100 |
---|---|---|
committer | spag <spag@golwen.net> | 2013-03-13 08:35:28 +0100 |
commit | a489e08d92e4266de08719b61c0dd4458b3334c8 (patch) | |
tree | 7f11d43c70199ea218873f60390ee907f67c7278 /misc/freeswitch/scripts/common | |
parent | 2ef4538bfc1e3fd9f118c64dab69b9c490b6a5bf (diff) |
to_json function added
Diffstat (limited to 'misc/freeswitch/scripts/common')
-rw-r--r-- | misc/freeswitch/scripts/common/array.lua | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/misc/freeswitch/scripts/common/array.lua b/misc/freeswitch/scripts/common/array.lua index 5683dce..b1b7a71 100644 --- a/misc/freeswitch/scripts/common/array.lua +++ b/misc/freeswitch/scripts/common/array.lua @@ -77,3 +77,21 @@ function keys_to_s(array, separator, prefix, suffix) return buffer; end + +-- convert to JSON +function to_json(array) + require 'common.str'; + local buffer = '{'; + for key, value in pairs(array) do + if type(value) == 'table' then + buffer = buffer .. '"' .. key .. '":' .. to_json(value) .. ','; + else + buffer = buffer .. '"' .. key .. '":' .. common.str.to_json(value) .. ','; + end + end + if buffer:sub(-1) == ',' then + buffer = buffer:sub(1, -2); + end + buffer = buffer .. '}'; + return buffer; +end |