[fluksod] optimize the json serialization

This commit is contained in:
Bart Van Der Meerssche 2011-05-01 20:23:14 +02:00
parent ab393a2c21
commit 6eb69c9a9b
1 changed files with 5 additions and 6 deletions

View File

@ -117,7 +117,7 @@ function json_encode(M, entries)
local J = {} local J = {}
if entries then if entries then
arr_size = 5*entries + 2 arr_size = 4*entries + 1
else else
arr_size = 0 arr_size = 0
end end
@ -125,19 +125,18 @@ function json_encode(M, entries)
for sensor, T in pairs(M) do for sensor, T in pairs(M) do
local H = timestamps(T) local H = timestamps(T)
-- use a string buffer for building up the JSON string
local SB = table.create(arr_size, 0) local SB = table.create(arr_size, 0)
SB[1] = '[' -- use a string buffer for building up the JSON string SB[1] = '[['
for k, timestamp in ipairs(H) do for k, timestamp in ipairs(H) do
SB[#SB+1] = '['
SB[#SB+1] = timestamp SB[#SB+1] = timestamp
SB[#SB+1] = ',' SB[#SB+1] = ','
SB[#SB+1] = T[timestamp] SB[#SB+1] = T[timestamp]
SB[#SB+1] = '],' SB[#SB+1] = '],['
end end
SB[#SB] = SB[#SB]:sub(1, -2) -- remove the trailing comma from the last entry SB[#SB] = ']]'
SB[#SB+1] = ']'
J[sensor] = table.concat(SB) J[sensor] = table.concat(SB)
end end