openwrt: use a string buffer for json_encode

This commit is contained in:
Bart Van Der Meerssche 2010-05-22 11:01:15 +02:00
parent fdd5363cf7
commit af4edb53e6
1 changed files with 5 additions and 3 deletions

View File

@ -88,12 +88,14 @@ end
function json_encode(M)
local J = {}
for meter, T in pairs(M) do
J[meter] = '['
local H = timestamps(T)
local SB = {'['} -- use a string buffer for building up the JSON string
for k, timestamp in ipairs(H) do
J[meter] = J[meter] .. '[' .. timestamp .. ',' .. T[timestamp] .. '],'
SB[#SB+1] = '[' .. timestamp .. ',' .. T[timestamp] .. '],'
end
J[meter] = string.sub(J[meter], 1, -2) .. ']'
SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trialing comma from the last entry
SB[#SB+1] = ']'
J[meter] = table.concat(SB)
end
return J
end