Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function serializeImpl(t) -- converts anything into a string
- local sType = type(t)
- if sType == "table" then
- local lstcnt=0
- for k,v in pairs(t) do
- lstcnt = lstcnt + 1
- end
- local result = "{"
- local aset=1
- local comma=false
- for k,v in pairs(t) do
- comma=true
- if k==aset then
- result = result..serializeImpl(v)..","
- aset=aset+1
- else
- result = result..("["..serializeImpl(k).."]="..serializeImpl(v)..",")
- end
- end
- if comma then
- result=string.sub(result,1,-2)
- end
- result = result.."}"
- return result
- elseif sType == "string" then
- return string.format("%q",t) -- improved from textutils
- elseif sType == "number" or sType == "boolean" or sType == "nil" then
- return tostring(t)
- elseif sType == "function" and not TF then -- function i added
- local status,data=pcall(string.dump,t) -- convert the function into a string
- if status then
- return 'func('..string.format("%q",data)..')' -- format it so it dosent screw up syntax
- else
- error()
- end
- elseif sType == "function" and TF then
- return tostring(t)
- else
- error()
- end
- end
- function split(T,func) -- splits a table
- if func then
- T=func(T) -- advanced function
- end
- local Out={}
- if type(T)=="table" then
- for k,v in pairs(T) do
- Out[split(k)]=split(v) -- set the values for the new table
- end
- else
- Out=T
- end
- return Out
- end
- function serialize( t ) -- TODO: combine with serializeImpl
- t=split(t)
- return serializeImpl( t, tTracking )
- end
- function unserialize(s,tf) -- converts a string back into its original form
- if type(s)~="string" then
- error("String exepcted. got "..type(s),2)
- end
- local func, e = loadstring( "return "..s, "serialize" )
- local funcs={} -- a table to store all the functions generated by f() (for securety)
- if not func then
- error("Invalid string.")
- end
- setfenv( func, { -- make sure nothing can be called within the function
- func=function(S) -- puts function requests into the funcs table
- local new={}
- funcs[new]=S
- return new
- end,
- })
- return split(func(),function(val) -- apply functions if any
- if funcs[val] then
- return loadstring(funcs[val])
- else
- return val
- end
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement