Advertisement
killer64

serialize

Dec 2nd, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. function serialize(dat,options)
  2.     options=options or {}
  3.     local out=""
  4.     local queue={{dat}}
  5.     local cv=0
  6.     local keydat
  7.     local ptbl={}
  8.     while queue[1] do
  9.         local cu=queue[1]
  10.         table.remove(queue,1)
  11.         local typ=type(cu[1])
  12.         local ot
  13.         if typ=="string" then
  14.             ot=string.gsub(string.format("%q",cu[1]),"\\\n","\\n")
  15.         elseif typ=="number" or typ=="boolean" or typ=="nil" then
  16.             ot=tostring(cu[1])
  17.         elseif typ=="table" then
  18.             local empty=true
  19.             ot="{"
  20.             local st=0
  21.             ptbl[#ptbl+1]=cu[1]
  22.             for k,v in pairs(cu[1]) do
  23.                 empty=false
  24.                 st=st+1
  25.                 table.insert(queue,st,{k,"key"})
  26.                 st=st+1
  27.                 local val=v
  28.                 if type(v)=="table" then
  29.                     for n,l in pairs(ptbl) do
  30.                         if l==v then
  31.                             if options.nofalse then
  32.                                 val="recursive"
  33.                             elseif options.noerror then
  34.                                 return false
  35.                             else
  36.                                 error("Cannot handle recursive tables.",2)
  37.                             end
  38.                         end
  39.                     end
  40.                 end
  41.                 table.insert(queue,st,{val,"value",nil,st/2})
  42.             end
  43.             if empty then
  44.                 ot=ot.."}"
  45.                 ptbl[#ptbl]=nil
  46.                 typ="emptytable"
  47.             else
  48.                 cv=cv+1
  49.                 if cu[3] then
  50.                     queue[st][3]=cu[3]
  51.                     cu[3]=nil
  52.                 end
  53.                 queue[st][3]=(queue[st][3] or 0)+1
  54.             end
  55.         elseif typ=="function" then
  56.             if options.nofunc then
  57.                 ot="function"
  58.             else
  59.                 local e,r,er=pcall(string.dump,cu[1])
  60.                 if e and r then
  61.                     ot="f(\""..r:gsub(".",function(c)
  62.                         return "\\"..tostring(string.byte(c)) end
  63.                     ).."\")"
  64.                 else
  65.                     if options.nofalse then
  66.                         ot="invalid function"
  67.                     elseif options.noerror then
  68.                         return false
  69.                     else
  70.                         error(r or er,2)
  71.                     end
  72.                 end
  73.             end
  74.         end
  75.         if cu[2]=="key" then
  76.             if type(ot)=="string" then
  77.                 local nt=ot:sub(2,-2)
  78.                 local e,r=loadstring("return {"..nt.."=true}")
  79.                 if options.noshortkey or not e then
  80.                     ot="["..ot.."]="
  81.                 else
  82.                     ot=nt.."="
  83.                 end
  84.             else
  85.                 ot=ot.."="
  86.             end
  87.             keydat={cu[1],ot}
  88.             ot=""
  89.         elseif cu[2]=="value" then
  90.             if keydat[1]~=cu[4] then
  91.                 ot=keydat[2]..ot
  92.             end
  93.             if cu[3] then
  94.                 ot=ot..("}"):rep(cu[3])
  95.                 for l1=1,cu[3] do
  96.                     ptbl[#ptbl]=nil
  97.                 end
  98.                 cv=cv-cu[3]
  99.                 if cv~=0 then
  100.                     ot=ot..","
  101.                 end
  102.             elseif typ~="table" then
  103.                 ot=ot..","
  104.             end
  105.         end
  106.         out=out..ot
  107.     end
  108.     return out
  109. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement