Advertisement
infiniteblock

Untitled

Apr 12th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. -- Version: 0.9.40
  2. -- This module is released under the MIT License (MIT).
  3. -----------------------------------------------------------------------------
  4. local d1,d2,d3,d4,d5,d6,d7,i1,i2,i3
  5. function encode(v) if v==nil then return 'null' end local t=type(v) if t=='string' then return '"'..i3(v)..'"' end if t=='number' or t=='boolean' then return tostring(v) end if t=='table' then local r={} local a1,m1=i1(v) if a1 then for i=1,m1 do table.insert(r,encode(v[i])) end else for i,j in pairs(v) do if i2(i) and i2(j) then table.insert(r,'"'..i3(i)..'":'..encode(j)) end end end if a1 then return '['..table.concat(r,',') ..']' else return '{'..table.concat(r,',')..'}' end end if t=='function' and v==null then return 'null' end assert(false,'encode attempt to encode unsupported type '..t..':'..tostring(v)) end
  6. function decode(s,s1) s1=s1 and s1 or 1 s1=d7(s,s1) assert(s1<=string.len(s),'Unterminated JSON encoded object found at position in ['..s..']') local c1=string.sub(s,s1,s1) if c1=='{' then return d5(s,s1) end if c1=='[' then return d1(s,s1) end if string.find("+-0123456789.e",c1,1,true) then return d4(s,s1) end if c1==[["]] or c1==[[']] then return d6(s,s1) end if string.sub(s,s1,s1+1)=='/*' then return decode(s,d2(s,s1)) end return d3(s,s1) end
  7. function d1(s,s1) local a3={} local sL=string.len(s) assert(string.sub(s,s1,s1)=='[','d1 called but array does not start at position '..s1..' in string:\n'..s) s1=s1+1 repeat s1=d7(s,s1) assert(s1<=sL,'JSON String ended unexpectedly scanning array.') local c1=string.sub(s,s1,s1) if (c1==']') then return a3,s1+1 end if c1==',' then s1=d7(s,s1+1) end assert(s1<=sL,'JSON String ended unexpectedly scanning array.') ox,s1=decode(s,s1) table.insert(a3,ox) until false end
  8. function d2(s,s1) assert(string.sub(s,s1,s1+1)=='/*',"d2 called but comment does not start at position "..s1) local e9=string.find(s,'*/',s1+2) assert(e9~=nil,"Unterminated comment in string at "..s1) return e9+2 end
  9. function d3(s,s1) local cT = {["true"]=true,["false"]=false,["null"]=nil} for i,k in pairs({"true","false","null"}) do if string.sub(s,s1,s1+string.len(k)-1)==k then return cT[k],s1+string.len(k) end end assert(nil,'Failed to scan constant from string '..s..' at starting position '..s1) end
  10. function d4(s,s1) local e9=s1+1 local sL=string.len(s) while(string.find('+-0123456789.e',string.sub(s,e9,e9),1,true) and e9<=sL) do e9=e9+1 end local sv='return '..string.sub(s,s1,e9-1) local eval1=loadstring(sv) assert(eval1,'Failed to scan number [ '..sv..'] in JSON string at position '..s1..' : '..e9) return eval1(),e9 end
  11. function d5(s,s1) local ob={} local sL=string.len(s) local k1,v1 assert(string.sub(s,s1,s1)=='{','d5 called but object does not start at position '..s1..' in string:\n'..s) s1=s1+1 repeat s1=d7(s,s1) assert(s1<=sL,'JSON string ended unexpectedly while scanning object.') local c1=string.sub(s,s1,s1) if c1=='}' then return ob,s1+1 end if c1==',' then s1=d7(s,s1+1) end assert(s1<=sL,'JSON string ended unexpectedly scanning object.') k1,s1=decode(s,s1) assert(s1<=sL,'JSON string ended unexpectedly searching for value of key '..k1) s1=d7(s,s1) assert(s1<=sL,'JSON string ended unexpectedly searching for value of key '..k1) assert(string.sub(s,s1,s1)==':','JSON object key-value assignment mal-formed at '..s1) s1=d7(s,s1+1) assert(s1<=sL,'JSON string ended unexpectedly searching for value of key '..k1) v1,s1=decode(s,s1) ob[k1]=v1 until false end
  12. function d6(s,s1) assert(s1,'d6(..) called without start position') local sp=string.sub(s,s1,s1) assert(sp==[[']] or sp==[["]],'d6 called for a non-string') local ef=false local e9=s1+1 local ez=false local sL=string.len(s) repeat local c1=string.sub(s,e9,e9) if not ef then if c1==[[\]] then ef=true else ez=c1==sp end else ef=false end e9=e9+1 assert(e9<=sL+1,"String decoding failed: unterminated string at position "..e9) until ez local sv='return '..string.sub(s,s1,e9-1) local eval2=loadstring(sv) assert(eval2,'Failed to load string [ '..sv..'] in JSON4Lua.d6 at position '..s1..' : '..e9) return eval2(),e9 end
  13. function d7(s,s1) local w1=" \n\r\t" local sL=string.len(s) while string.find(w1,string.sub(s,s1,s1),1,true) and s1<=sL do s1=s1+1 end return s1 end
  14. function i3(s) s=string.gsub(s,'\\','\\\\') s=string.gsub(s,'"','\\"') s=string.gsub(s,'\n','\\n') s=string.gsub(s,'\t','\\t') return s end
  15. function i1(t) local i9=0 for k,v in pairs(t) do if type(k)=='number' and math.floor(k)==k and 1<=k then if not i2(v) then return false end i9=math.max(i9,k) else if k=='n' then if v~=table.getn(t) then return false end else if i2(v) then return false end end end end return true,i9 end
  16. function i2(o) local t=type(o) return (t=='string' or t=='boolean' or t=='number' or t=='nil' or t=='table') or (t=='function' and o==null) end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement