Advertisement
Antwanr942

Java Serialization

Jul 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.38 KB | None | 0 0
  1. local serialization = {}
  2.  
  3. function serialization.serialize(value)
  4.   local kw =  {["and"]=true, ["break"]=true, ["do"]=true, ["else"]=true,
  5.                ["elseif"]=true, ["end"]=true, ["false"]=true, ["for"]=true,
  6.                ["function"]=true, ["goto"]=true, ["if"]=true, ["in"]=true,
  7.                ["local"]=true, ["nil"]=true, ["not"]=true, ["or"]=true,
  8.                ["repeat"]=true, ["return"]=true, ["then"]=true, ["true"]=true,
  9.                ["until"]=true, ["while"]=true}
  10.   local id = "^[%a_][%w_]*$"
  11.   local ts = {}
  12.   local function s(v, l)
  13.     local t = type(v)
  14.     if t == "nil" then
  15.       return "nil"
  16.     elseif t == "boolean" then
  17.       return v and "true" or "false"
  18.     elseif t == "number" then
  19.       if v ~= v then
  20.         return "0/0"
  21.       elseif v == math.huge then
  22.         return "math.huge"
  23.       elseif v == -math.huge then
  24.         return "-math.huge"
  25.       else
  26.         return tostring(v)
  27.       end
  28.     elseif t == "string" then
  29.       return string.format("%q", v):gsub("\\\n","\\n")
  30.     elseif t == "table" then
  31.       if ts[v] then
  32.         error("tables with cycles are not supported")
  33.       end
  34.       ts[v] = true
  35.       local i, r = 1, nil
  36.       local f
  37.       f = table.pack(pairs(v))
  38.       for k, v in table.unpack(f) do
  39.         if r then
  40.           r = r .. ","
  41.         else
  42.           r = "{"
  43.         end
  44.         local tk = type(k)
  45.         if tk == "number" and k == i then
  46.           i = i + 1
  47.           r = r .. s(v, l + 1)
  48.         else
  49.           if tk == "string" and not kw[k] and string.match(k, id) then
  50.             r = r .. k
  51.           else
  52.             r = r .. "[" .. s(k, l + 1) .. "]"
  53.           end
  54.           r = r .. "=" .. s(v, l + 1)
  55.         end
  56.       end
  57.       ts[v] = nil -- allow writing same table more than once
  58.       return (r or "{") .. "}"
  59.     else
  60.       error("unsupported type: " .. t)
  61.     end
  62.   end
  63.   local result = s(value, 1)
  64.   return result
  65. end
  66.  
  67. function serialization.unserialize(data)
  68.   checkArg(1, data, "string")
  69.   local result, reason = load("return " .. data, "=data", _, {math={huge=math.huge}})
  70.   if not result then
  71.     return nil, reason
  72.   end
  73.   local ok, output = pcall(result)
  74.   if not ok then
  75.     return nil, output
  76.   end
  77.   return output
  78. end
  79.  
  80. -- This serialzier is bad, it is supposed to be bad. Don't use it.
  81. function serialization.javaserialize(t)
  82.     local tTracking = {}
  83.     local function serializeImpl(t)
  84.         local sType = type(t)
  85.         if sType == "table" then
  86.             if tTracking[t] ~= nil then
  87.                 return nil
  88.             end
  89.             tTracking[t] = true
  90.  
  91.             local result = "{"
  92.             for k,v in pairs(t) do
  93.                 local cache1 = serializeImpl(k)
  94.                 local cache2 = serializeImpl(v)
  95.                 result = result..cache1.."="..cache2..", "
  96.             end
  97.             if result:sub(-2,-1) == ", " then result = result:sub(1,-3) end
  98.             result = result.."}"
  99.             return result
  100.         elseif sType == "string" then
  101.             return t
  102.         elseif sType == "number" then
  103.             if t == math.huge then
  104.                 return "Infinity"
  105.             elseif t == -math.huge then
  106.                 return "-Infinity"
  107.             elseif t ~= t then
  108.                 return "NaN"
  109.             else
  110.                 return tostring(t):gsub("^[^e.]+%f[^0-9.]","%1.0"):gsub("e%+","e"):upper()
  111.             end
  112.         elseif sType == "boolean" then
  113.             return tostring(t)
  114.         else
  115.             return string.format("%s@%x", "li.cil.repack.com.naef.jnlua.LuaState$LuaValueProxyImpl", math.random(0, 0xffffffff))
  116.         end
  117.     end
  118.     return serializeImpl(t)
  119. end
  120.  
  121. return serialization
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement