Saltu al enhavo

Modulo:SerializeTable

El Vikinovaĵoj, libera retgazeto

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:SerializeTable/dokumentado

--[[
------------------------------------------
--              SerializeTable
-- Модуль для сериализации Lua-таблицы.
------------------------------------------
--]]

local SerializeTable = {}

function SerializeTable.go(atable, level)
 if(type(atable) ~= "table") then return atable end
 local s = "{"
 local kr, vr
--[[ --]]
 if(level == nil) then level = 1 else level = level + 1 end
 local first = true
 for k, v in pairs(atable) do
  if (first) then first = false else s = s .. ", " end
  s = s .. "<br>"
  for i = 1, level*2 do s = s .. "&nbsp;" end
  kr = k
  if (type(k) == "table") then
   kr = SerializeTable.go(k, level)
  elseif (type(k) == "number") then
   kr = nil
  end
  vr = v
  if (type(v) == "table") then
   vr = SerializeTable.go(v, level)
  elseif (type(v) == "string") then
   vr = "\"" ..v .. "\""
  end
  if (kr == nil) then
   s = s .. " " .. vr
  else
   s = s .. " " .. kr .. " = " .. vr
  end
 end
 s = s .. "<br>"
 for i = 1, level*2 do s = s .. "&nbsp;" end
 s = s .. "}"
 return s
end

function SerializeTable.test()
 -- local at = {"1", "2"}
 -- local at = {1, 2}
 -- local at = {1, 2, "1", "2", a = 1, b = "2"}
 -- local at = {1, 2, {"1", "2"}}
 local at = {1, 2, "1", "2", a = 1, b = "2", {"1", "2"}, code = {3, "4"}, double = {7, one = {1, 2, 3}}}
 return SerializeTable.go(at)
end

return SerializeTable