|
|
Serialize nonrecursive data structureSummary
Since it uses the lua interpreter itself for parsing the results, it loads even large datasets quickly. It is also very useful for debugging, since the serialized data structure can be dumped to screen or file. Example
returns
DownloadhereCode-- ##########################################################################
-- # dump() function for lua
-- # Markus Nentwig 2007
-- # This code is in the public domain and provided without any warranty.
-- #
-- # Serializes a table tree structure (non-recursive)
-- # executing "return "..dump(struct) reconstructs the structure.
-- ##########################################################################
local dump
dump=
function(a)
local t=type(a)
if t=="nil" then
return "nil"
elseif t=="number" then
return tostring(a)
elseif t=="string" then
return string.format("%q", a)
elseif t=="table" then
local res={}
-- do itable part
local done={}
for key, val in ipairs(a) do
table.insert(res, dump(val))
done[key]=true
end
for key, val in pairs(a) do
if not done[key] then
table.insert(res, "["..dump(key).."]="..dump(val))
end
end
return "{"..table.concat(res, ",").."}"
else
error("invalid type "..(t or "?"))
end
end
return dump
© Markus Nentwig 2007-2008 The content of this page is provided without any warranty and may not be reproduced without permission. Comments? Questions?Please send me a mail! mnentwig@elisanet.fi |