Guest User

Untitled

a guest
Feb 18th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.00 KB | None | 0 0
  1. function table.serialize(t)
  2.     if not t or type(t) ~= 'table' then return end -- if nil or not a table
  3.     local buf = '{'
  4.     for key,value in pairs(t) do
  5.         local v_type,k_type = type(value),type(key)
  6.         if      v_type ~= 'userdata'    and k_type ~= 'userdata' -- ignore fields and keys witch contain userdata, thread or function
  7.         and     v_type ~= 'thread'      and k_type ~= 'thread'
  8.         and     v_type ~= 'function'    and k_type ~= 'function'     
  9.         then
  10.             if k_type == 'number' then  
  11.                 buf = buf .. '['..key..'] = '
  12.             else
  13.                 buf = buf .. '[\''..key..'\'] = ' end
  14.             if v_type == 'table' then
  15.                 value = table.serialize(value)
  16.             elseif v_type == 'string' then
  17.                 value = '\''..value..'\''
  18.             else
  19.                 value = tostring(value)
  20.             end
  21.             buf = buf .. value
  22.             if next(t,key) then buf = buf..',' end
  23.         end
  24.     end
  25.     return buf .. '}'
  26. end
  27.  
  28. function table.deserialize(s)
  29.     local getTableFromString = loadstring('return '..s)
  30.     local t = getTableFromString()
  31.     if type(t) ~= 'table' then return end
  32.     return t
  33. end
Advertisement
Add Comment
Please, Sign In to add comment