From a489e08d92e4266de08719b61c0dd4458b3334c8 Mon Sep 17 00:00:00 2001 From: spag Date: Wed, 13 Mar 2013 08:35:28 +0100 Subject: to_json function added --- misc/freeswitch/scripts/common/array.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 -- cgit v1.2.3