Advertisement
guitarplayer616

Textutils Serialize Fix!

Jan 14th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.34 KB | None | 0 0
  1. local g_tLuaKeywords = {
  2.     [ "and" ] = true,
  3.     [ "break" ] = true,
  4.     [ "do" ] = true,
  5.     [ "else" ] = true,
  6.     [ "elseif" ] = true,
  7.     [ "end" ] = true,
  8.     [ "false" ] = true,
  9.     [ "for" ] = true,
  10.     [ "function" ] = true,
  11.     [ "if" ] = true,
  12.     [ "in" ] = true,
  13.     [ "local" ] = true,
  14.     [ "nil" ] = true,
  15.     [ "not" ] = true,
  16.     [ "or" ] = true,
  17.     [ "repeat" ] = true,
  18.     [ "return" ] = true,
  19.     [ "then" ] = true,
  20.     [ "true" ] = true,
  21.     [ "until" ] = true,
  22.     [ "while" ] = true,
  23. }
  24.  
  25. local function serializeImpl( t, tTracking, sIndent )
  26.     local sType = type(t)
  27.     if sType == "table" then
  28.         --if tTracking[t] ~= nil then
  29.             --error( "Cannot serialize table with recursive entries", 0 )
  30.         --end
  31.         tTracking[t] = true
  32.  
  33.         if next(t) == nil then
  34.             -- Empty tables are simple
  35.             return "{}"
  36.         else
  37.             -- Other tables take more work
  38.             local sResult = "{\n"
  39.             local sSubIndent = sIndent .. "  "
  40.             local tSeen = {}
  41.             for k,v in ipairs(t) do
  42.                 tSeen[k] = true
  43.                 sResult = sResult .. sSubIndent .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  44.             end
  45.             for k,v in pairs(t) do
  46.                 if not tSeen[k] then
  47.                     local sEntry
  48.                     if type(k) == "string" and not g_tLuaKeywords[k] and string.match( k, "^[%a_][%a%d_]*$" ) then
  49.                         sEntry = k .. " = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  50.                     else
  51.                         sEntry = "[ " .. serializeImpl( k, tTracking, sSubIndent ) .. " ] = " .. serializeImpl( v, tTracking, sSubIndent ) .. ",\n"
  52.                     end
  53.                     sResult = sResult .. sSubIndent .. sEntry
  54.                 end
  55.             end
  56.             sResult = sResult .. sIndent .. "}"
  57.             return sResult
  58.         end
  59.        
  60.     elseif sType == "string" then
  61.         return string.format( "%q", t )
  62.    
  63.     else
  64.         return tostring(t)
  65.    
  66.     end
  67. end
  68.  
  69. local EMPTY_ARRAY = {}
  70.  
  71. local function serializeJSONImpl( t, tTracking )
  72.     local sType = type(t)
  73.     if t == EMPTY_ARRAY then
  74.         return "[]"
  75.  
  76.     elseif sType == "table" then
  77.  
  78.         tTracking[t] = true
  79.  
  80.         if next(t) == nil then
  81.             -- Empty tables are simple
  82.             return "{}"
  83.         else
  84.             -- Other tables take more work
  85.             local sObjectResult = "{"
  86.             local sArrayResult = "["
  87.             local nObjectSize = 0
  88.             local nArraySize = 0
  89.             for k,v in pairs(t) do
  90.                 if type(k) == "string" then
  91.                     local sEntry = serializeJSONImpl( k, tTracking ) .. ":" .. serializeJSONImpl( v, tTracking )
  92.                     if nObjectSize == 0 then
  93.                         sObjectResult = sObjectResult .. sEntry
  94.                     else
  95.                         sObjectResult = sObjectResult .. "," .. sEntry
  96.                     end
  97.                     nObjectSize = nObjectSize + 1
  98.                 end
  99.             end
  100.             for n,v in ipairs(t) do
  101.                 local sEntry = serializeJSONImpl( v, tTracking )
  102.                 if nArraySize == 0 then
  103.                     sArrayResult = sArrayResult .. sEntry
  104.                 else
  105.                     sArrayResult = sArrayResult .. "," .. sEntry
  106.                 end
  107.                 nArraySize = nArraySize + 1
  108.             end
  109.             sObjectResult = sObjectResult .. "}"
  110.             sArrayResult = sArrayResult .. "]"
  111.             if nObjectSize > 0 or nArraySize == 0 then
  112.                 return sObjectResult
  113.             else
  114.                 return sArrayResult
  115.             end
  116.         end
  117.  
  118.     elseif sType == "string" then
  119.         return string.format( "%q", t )
  120.  
  121.     else
  122.         return tostring(t)
  123.  
  124.     end
  125. end
  126.  
  127. function serialize( t )
  128.     local tTracking = {}
  129.     return serializeImpl( t, tTracking, "" )
  130. end
  131.  
  132. function unserialize( s )
  133.     local func = loadstring( "return "..s, "unserialize" )
  134.     if func then
  135.         setfenv( func, {} )
  136.         local ok, result = pcall( func )
  137.         if ok then
  138.             return result
  139.         end
  140.     end
  141.     return nil
  142. end
  143.  
  144. function serializeJSON( t )
  145.     local tTracking = {}
  146.     return serializeJSONImpl( t, tTracking )
  147. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement