SHARE
TWEET

Serialize table to string

Krutoy242 Apr 16th, 2015 363 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ********************************************************************************** --
  2. -- **   Serialize table to string                                                  ** --
  3. -- **                                                                              ** --
  4. -- **   Modified version of http://lua-users.org/wiki/SaveTableToFile              ** --
  5. -- **                                                                              ** --
  6. -- ********************************************************************************** --
  7.  
  8. -- declare local variables
  9. --// exportstring( string )
  10. --// returns a "Lua" portable version of the string
  11. local function exportstring( s )
  12.   return string.format("%q", s)
  13. end
  14.  
  15. local insert  = table.insert
  16. local tostring= tostring
  17. local ipairs  = ipairs
  18. local pairs   = pairs
  19. local type    = type
  20.  
  21. --// The Save Function
  22. function table.toString(tbl)
  23.   if type(tbl) ~= 'table' then return "" end -- Argument not a table
  24.  
  25.   local charS,charE = "   ","\n"
  26.   local s_tbl = {}
  27.  
  28.   -- initiate variables for save procedure
  29.   local tables,lookup = { tbl },{ [tbl] = 1 }
  30.   insert(s_tbl, "return {"..charE )
  31.  
  32.   for idx,t in ipairs( tables ) do
  33.     insert(s_tbl, "-- Table: {"..idx.."}"..charE )
  34.     insert(s_tbl, "{"..charE )
  35.     local thandled = {}
  36.  
  37.     for i,v in ipairs( t ) do
  38.       thandled[i] = true
  39.       local stype = type( v )
  40.       -- only handle value
  41.       if stype == "table" then
  42.         if not lookup[v] then
  43.           insert( tables, v )
  44.           lookup[v] = #tables
  45.         end
  46.         insert(s_tbl, charS.."{"..lookup[v].."},"..charE )
  47.       elseif stype == "string" then
  48.         insert(s_tbl,  charS..exportstring( v )..","..charE )
  49.       elseif stype == "number" or stype == "boolean" then
  50.         insert(s_tbl,  charS..tostring( v )..","..charE )
  51.       end
  52.     end
  53.  
  54.     for i,v in pairs( t ) do
  55.       -- escape handled values
  56.       if (not thandled[i]) then
  57.  
  58.         local str = ""
  59.         local stype = type( i )
  60.         -- handle index
  61.         if stype == "table" then
  62.           if not lookup[i] then
  63.             insert( tables,i )
  64.             lookup[i] = #tables
  65.           end
  66.           str = charS.."[{"..lookup[i].."}]="
  67.         elseif stype == "string" then
  68.           str = charS.."["..exportstring( i ).."]="
  69.         elseif stype == "number" or stype == "boolean" then
  70.           str = charS.."["..tostring( i ).."]="
  71.         end
  72.  
  73.         if str ~= "" then
  74.           stype = type( v )
  75.           -- handle value
  76.           if stype == "table" then
  77.             if not lookup[v] then
  78.               insert( tables,v )
  79.               lookup[v] = #tables
  80.             end
  81.             insert(s_tbl, str.."{"..lookup[v].."},"..charE )
  82.           elseif stype == "string" then
  83.             insert(s_tbl, str..exportstring( v )..","..charE )
  84.           elseif stype == "number" or stype == "boolean" then
  85.             insert(s_tbl, str..tostring( v )..","..charE )
  86.           end
  87.         end
  88.       end
  89.     end
  90.     insert(s_tbl, "},"..charE )
  91.   end
  92.   insert(s_tbl, "}" )
  93.  
  94.   return table.concat(s_tbl)
  95. end
  96.  
  97. --// The Load Function
  98. function table.fromString(s)
  99.   if not s then return end -- Argument not string
  100.   local ftables = loadstring(s)
  101.   if not ftables then return end -- String cant be parsed into function
  102.   local tables = ftables()
  103.   for idx = 1,#tables do
  104.     local tolinki = {}
  105.     for i,v in pairs( tables[idx] ) do
  106.       if type( v ) == "table" then
  107.         tables[idx][i] = tables[v[1]]
  108.       end
  109.       if type( i ) == "table" and tables[i[1]] then
  110.         insert( tolinki,{ i,tables[i[1]] } )
  111.       end
  112.     end
  113.     -- link indices
  114.     for _,v in ipairs( tolinki ) do
  115.       tables[idx][v[2]],tables[idx][v[1]] =  tables[idx][v[1]],nil
  116.     end
  117.   end
  118.   return tables[1]
  119. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top