Andy73

JSON

Feb 21st, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.72 KB | None | 0 0
  1. local B,E=string,table local d,e,C,D,F,G,H,I=assert,B.sub,B.gsub,B.find,E.insert,E.concat,'JSON string ended unexpectedly ',' at position '
  2. local function d_sW(s,a)local b=#s while(D("\n\r\t",e(s,a,a),1,true)and a<=b)do a=a+1 end return a end
  3. local function d_sA(s,a)local A={}local b=#s d(e(s,a,a)=='[','d_sA called but array does not start'..I..a..' in string:\n'..s)a=a+1 repeat a=d_sW(s,a)d(a<=b,H..'scanning array.')local f=e(s,a,a)if(f==']')then return A,a+1 end if(f==',')then a=d_sW(s,a+1)end d(a<=b,H..'scanning array.')n,a=decode(s,a)F(A,n)until false end
  4. local function d_sC(s,a)d(e(s,a,a+1)=='/*','d_sC called but comment does not start'..I..a)local c=D(s,'*/',a+2)d(c~=nil,'Unterminated comment in string at '..a)return c+2 end
  5. local function d_sC(s,a)local z={['true']=true,['false']=false,['null']=nil}for i,k in pairs({'true','false','null'})do if e(s,a,a+#k -1)==k then return z[k],a+#k end end d(nil,'Failed to scan constant from string '..s..' at starting position '..a)end
  6. local function d_sN(s,a)local c=a+1 local b=#s while(D('+-0123456789.e',e(s,c,c),1,true)and c<=b)do c=c+1 end local h='return '..e(s,a,c-1)local g=loadstring(h)d(g,'Failed to scan number [ '..h..'] in JSON string'..I..a..' : '..c)return g(),c end
  7. local function d_sO(s,a)local n={}local b=#s local m,p d(e(s,a,a)=='{','d_sO called but object does not start'..I..a..' in string:\n'..s)a=a+1 repeat a=d_sW(s,a)d(a<=b,H..'scanning object.')local f=e(s,a,a)if(f=='}')then return n,a+1 end if(f==',')then a=d_sW(s,a+1)end d(a<=b,H..'scanning object.')m,a=decode(s,a)d(a<=b,H..'searching for value of key '..m)a=d_sW(s,a)d(a<=b,H..'searching for value of key '..m)d(e(s,a,a)==':','JSON object key-value assignment mal-formed at '..a)a=d_sW(s,a+1)d(a<=b,H..'searching for value of key '..m)p,a=decode(s,a)n[m]=p until false end
  8. local function d_sS(s,a)d(a,'d_sS called without start position')local q=e(s,a,a)d(q=="'"or q=='"','d_sS called for a non-string')local x=false local c=a+1 local y=false local b=#s repeat local f=e(s,c,c)if not x then if f=='\\'then x=true else y=f==q end else x=false end c=c+1 d(c<=b+1,'String decoding failed: unterminated string'..I..c)until y local h='return '..e(s,a,c-1)local g=loadstring(h)d(g,'Failed to load string [ '..h..'] in JSON4Lua.d_sS'..I..a..' : '..c)return g(),c end
  9. local function eS(s)s=C(s,'\\','\\\\')s=C(s,'"','\\"')s=C(s,"'","\\'")s=C(s,'\n','\\n')s=C(s,'\t','\\t')return s end
  10. local function iE(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
  11. local function iA(t)local l=0 for k,v in pairs(t)do if(type(k)=='number'and math.floor(k)==k and 1<=k)then if(not iE(v))then return false end l=math.max(l,k)else if(k=='n')then if v~=E.getn(t)then return false end else if iE(v)then return false end end end end return true,l end
  12. function encode(v)if v==nil then return'null'end local w=type(v)if w=='string'then return'"'..eS(v)..'"'end if w=='number'or w=='boolean'then return tostring(v)end if w=='table'then local rval={}local u,r=iA(v)if u then for i=1,r do F(rval,encode(v[i]))end else for i,j in pairs(v)do if iE(i)and iE(j)then F(rval,'"'..eS(i)..'":'..encode(j))end end end if u then return'['..G(rval,',')..']'else return'{'..G(rval,',')..'}'end end if w=='function'and v==null then return'null'end d(false,'encode attempt to encode unsupported type '..w..':'..tostring(v))end
  13. function decode(s,a)a=a and a or 1 a=d_sW(s,a)d(a<=#s,'Unterminated JSON encoded object found'..I..'in ['..s..']')local f=e(s,a,a)if f=='{'then return d_sO(s,a)end if f=='['then return d_sA(s,a)end if D('+-0123456789.e',f,1,true)then return d_sN(s,a)end if f=='"'or f=="'"then return d_sS(s,a)end if e(s,a,a+1)=='/*'then return decode(s,d_sC(s,a))end return d_sC(s,a)end
  14. function null()return null end
Advertisement
Add Comment
Please, Sign In to add comment