Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function table.serialize(t)
- if not t or type(t) ~= 'table' then return end -- if nil or not a table
- local buf = '{'
- for key,value in pairs(t) do
- local v_type,k_type = type(value),type(key)
- if v_type ~= 'userdata' and k_type ~= 'userdata' -- ignore fields and keys witch contain userdata, thread or function
- and v_type ~= 'thread' and k_type ~= 'thread'
- and v_type ~= 'function' and k_type ~= 'function'
- then
- if k_type == 'number' then
- buf = buf .. '['..key..'] = '
- else
- buf = buf .. '[\''..key..'\'] = ' end
- if v_type == 'table' then
- value = table.serialize(value)
- elseif v_type == 'string' then
- value = '\''..value..'\''
- else
- value = tostring(value)
- end
- buf = buf .. value
- if next(t,key) then buf = buf..',' end
- end
- end
- return buf .. '}'
- end
- function table.deserialize(s)
- local getTableFromString = loadstring('return '..s)
- local t = getTableFromString()
- if type(t) ~= 'table' then return end
- return t
- end
Advertisement
Add Comment
Please, Sign In to add comment