|
One way to serialize tables in Lua is to write the actual table constructor text into the file. This has the advantage that you can then use Lua itself to load the table from the file contents. This is a simplified version that does not handle nested tables: function table.pickle(t, fileName) local output = io.open(fileName, "w") output:write("return {\n") for k, v in pairs(t) do if (type(k) == 'string' or type(k) == 'number' or type(k) == 'boolean') and (type(v) == 'string' or type(v) == 'number' or type(v) == 'boolean'') then if type(k) == 'string' then k = string.format("%q", k) else k = tostring(k) end if type(v) == 'string' then v = string.format("%q", v) else v = tostring(v) end output:write(string.format("[%s] = %s,\n", k, v) end end output:close() end NFF |