I am wanting to convert a 2d lua table into a string, then after converting it to a string convert it back into a table using that newly created string. It seems as if this process is called serialization, and is discussed in the below url, yet I am having a difficult time understanding the code and was hoping someone here had a simple table.toString and table.fromString function
http://lua-users.org/wiki/TableSerialization
8 Answers
I am using the following code in order to serialize tables:
function serializeTable(val, name, skipnewlines, depth) skipnewlines = skipnewlines or false depth = depth or 0 local tmp = string.rep(" ", depth) if name then tmp = tmp .. name .. " = " end if type(val) == "table" then tmp = tmp .. "{" .. (not skipnewlines and "\n" or "") for k, v in pairs(val) do tmp = tmp .. serializeTable(v, k, skipnewlines, depth + 1) .. "," .. (not skipnewlines and "\n" or "") end tmp = tmp .. string.rep(" ", depth) .. "}" elseif type(val) == "number" then tmp = tmp .. tostring(val) elseif type(val) == "string" then tmp = tmp .. string.format("%q", val) elseif type(val) == "boolean" then tmp = tmp .. (val and "true" or "false") else tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" end return tmp end the code created can then be executed using loadstring(): http://www.lua.org/manual/5.1/manual.html#pdf-loadstring if you have passed an argument to 'name' parameter (or append it afterwards):
s = serializeTable({a = "foo", b = {c = 123, d = "foo"}}) print(s) a = loadstring(s)() 3 Comments
a={}; a[1]=2; however. ({1=2} is not dostring-able) Also for nil etc.. For pretty printing there are already other libraries however.{1=...} I added this line: if type(k) == "number" then k = "[\"" .. tostring(k) .. "\"]" end in the for loop.The code lhf posted is a much simpler code example than anything from the page you linked, so hopefully you can understand it better. Adapting it to output a string instead of printing the output looks like:
t = { {11,12,13}, {21,22,23}, } local s = {"return {"} for i=1,#t do s[#s+1] = "{" for j=1,#t[i] do s[#s+1] = t[i][j] s[#s+1] = "," end s[#s+1] = "}," end s[#s+1] = "}" s = table.concat(s) print(s) The general idea with serialization is to take all the bits of data from some data structure like a table, and then loop through that data structure while building up a string that has all of those bits of data along with formatting characters.
6 Comments
print or using a different function to collect the output in a table so that you could keep my original code mostly intact.#t has to do some iterating to calculate the length of a table. It may be more efficient to use your own length-counter variable.How about a JSON module? That way you have also a better exchangeable data. I usually prefer dkjson, which also supports utf-8, where cmjjson won't.
1 Comment
Under the kong works this
local cjson = require "cjson" kong.log.debug(cjson.encode(some_table)) Out of the kong should be installed package lua-cjson https://github.com/openresty/lua-cjson/
Comments
Here is a simple program which assumes your table contains numbers only. It outputs Lua code that can be loaded back with loadstring()(). Adapt it to output to a string instead of printing it out. Hint: redefine print to collect the output into a table and then at the end turn the output table into a string with table.concat.
t = { {11,12,13}, {21,22,23}, } print"return {" for i=1,#t do print"{" for j=1,#t[i] do print(t[i][j],",") end print"}," end print"}" Comments
Assuming that:
- You don't have loops (table a referencing table b and b referencing a)
- Your tables are pure arrays (all keys are consecutive positive integers, starting on 1)
- Your values are integers only (no strings, etc)
Then a recursive solution is easy to implement:
function serialize(t) local serializedValues = {} local value, serializedValue for i=1,#t do value = t[i] serializedValue = type(value)=='table' and serialize(value) or value table.insert(serializedValues, serializedValue) end return string.format("{ %s }", table.concat(serializedValues, ', ') ) end Prepend the string resulting from this function with a return, store it on a .lua file:
-- myfile.lua return { { 1, 2, 3 }, { 4, 5, 6 } } You can just use dofile to get the table back.
t = dofile 'myfile.lua' Notes:
- If you have loops, then you will have to handle them explicitly - usually with an extra table to "keep track" of repetitions
- If you don't have pure arrays, then you will have to parse t differently, as well as handle the way the keys are rendered (are they strings? are they other tables? etc).
- If you have more than just integers and subtables, then calculating
serializedValuewill be more complex.
Regards!
Comments
I have shorter code to convert table to string but not reverse
function compileTable(table) local index = 1 local holder = "{" while true do if type(table[index]) == "function" then index = index + 1 elseif type(table[index]) == "table" then holder = holder..compileTable(table[index]) elseif type(table[index]) == "number" then holder = holder..tostring(table[index]) elseif type(table[index]) == "string" then holder = holder.."\""..table[index].."\"" elseif table[index] == nil then holder = holder.."nil" elseif type(table[index]) == "boolean" then holder = holder..(table[index] and "true" or "false") end if index + 1 > #table then break end holder = holder.."," index = index + 1 end return holder.."}" end if you want change the name just search all compileTable change it to you preferred name because this function will call it self if it detect nested table but escape sequence I don't know if it work
if you use this to create a lua executable file that output the table it will ge compilation error if you put new line and " sequence this method is more memory efficient
Note:
- Function not supported
- User data I don't know
Comments
My solution:
local nl = string.char(10) -- newline function serialize_list (tabl, indent) indent = indent and (indent.." ") or "" local str = '' str = str .. indent.."{" for key, value in pairs (tabl) do local pr = (type(key)=="string") and ('["'..key..'"]=') or "" if type (value) == "table" then str = str..nl..pr..serialize_list (value, indent)..',' elseif type (value) == "string" then str = str..nl..indent..pr..'"'..tostring(value)..'",' else str = str..nl..indent..pr..tostring(value)..',' end end str = str:sub(1, #str-1) -- remove last symbol str = str..nl..indent.."}" return str end local str = serialize_list(tables) print('return '..nl..str)