Advertisement
TheIncgi

Concise Object Notation

Apr 1st, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.48 KB | None | 0 0
  1. --Concise Object Notation - SpiderString - TheINCGI
  2. local CON = {}
  3.  
  4. local function exportObj( file, obj, indent, name )
  5.   indent = indent or 0
  6.   file.write(("  "):rep(indent)..name)
  7.   if type(obj)~="table" then
  8.     file.writeLine((type(name)=="number" and " = " or ": ").. (type(obj)=="string" and ('"'..obj..'"') or tostring(obj)))
  9.   else
  10.     file.writeLine("")
  11.     for k,v in pairs( obj ) do
  12.       exportObj( file, v, indent+1, k )
  13.     end
  14.   end
  15. end
  16.  
  17. local function importObj( file, out, line, indents )
  18.   if not line then return end
  19.   indents = indents or 0
  20.   local name = line:gsub("^ *",""):gsub(" *$","")
  21.   local t;
  22.   if not name:match"^.+ ?[=:]" then
  23.     t = {}
  24.     out[ name ] = t
  25.     if file.available() <= 0 then return end
  26.     line = file.readLine():gsub(" *$","") --trim end
  27.   else
  28.     t = out
  29.     line = "  "..line
  30.   end
  31.   while line and #line:gmatch(" *")() / 2 == indents + 1 do
  32.     local m = line:match"^ *%w+ ?[:=] "
  33.     if m then --any spaces, var name, and a : space
  34.       local keyIsNum = m:match"^ *-?%w+ =" and true or false
  35.       local pName = line:match"^ *(-?%w+) ?[:=]"
  36.       local v = line:sub(#m+1)
  37.       local s = v:match"^%\"(.+)%\""
  38.       if s then
  39.         v = s
  40.       else
  41.         if v=="true" then
  42.           v = true  
  43.         elseif v=="false" then
  44.           v = false  
  45.         else
  46.           local n = tonumber(v)
  47.           if not n then error(string.format("Format exception for field '%s' with value '%s'",pName,v)) end
  48.           v = n
  49.         end
  50.       end
  51.       t[ keyIsNum and tonumber(pName) or pName ] = v
  52.       line = file.readLine():gsub(" $","")
  53.     else
  54.       line = importObj( file, t, line, indents+1 )
  55.     end
  56.   end
  57.   return line
  58. end
  59.  
  60. function CON.import( path )
  61.   local out = {}
  62.   local file = filesystem.open( path, "r" )
  63.   local line = file.readLine()
  64.   while file.available() > 0 do
  65.     line = importObj( file, out, line )
  66.   end
  67.   file.close()
  68.   return out
  69. end
  70.  
  71. function CON.export( path, obj )
  72.   assert( type(path)=="string", "String path expected, got "..type(path) )
  73.   assert( type(obj)=="table",   "Table expected, got "..type(obj) )
  74.  
  75.   local file = filesystem.open( path, "w" )
  76.   for k,v in pairs(obj) do
  77.     --if type(v)=="table" then
  78.       exportObj( file, v, 0, k )
  79.     --end
  80.   end
  81.   file.close()
  82. end
  83.  
  84. local test = {
  85.   x = {
  86.    p={},
  87.     1,2,3
  88.   },
  89.   y = {
  90.     h="i"
  91.   },
  92.   z= {56},
  93.   w= {"ok", toast="most"},
  94.   l=100
  95. }
  96.  
  97. CON.export( "test.con", test )
  98. log( CON.import( "test.con" ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement