本文主要是介绍阿赵Json工具AzhaoJson的Lua版本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
大家好,我是阿赵。
之前分享了AzhaoJson的C#版本,这里顺便分享一下Lua的版本:
AzhaoJson.lua:
require "util/jsonParser"AzhaoJson = {}--lua table转json字符串
function AzhaoJson.Encode( tab )local str = jsonParser.encode(tab)return str
end--json字符串转lua table
function AzhaoJson.Decode( str)local tab = jsonParser.parser(tostring(str))return tab
end
由于Lua是弱类型的,所以转换起来很简单,不需要用反射,只需要table转string,或者string转table就行了。所以主要的工具类里面只提供了这两个方法。
然后实际实现的类是jsonParser.lua:
jsonParser = {};
local this = jsonParserfunction jsonParser.parser( str )this.curPos = 1this.orgStr = strthis.len = string.len(str)this.token = this.read_un_space()if this.token == "{" thenthis.tab = this.get_obj()elseif this.token == "[" thenthis.tab = this.get_array()endif this.tab~=nil thenreturn this.tabelsereturn nilend
endfunction jsonParser.read_un_space( )if this.curPos>=this.len thenreturn ""endlocal s = string.sub(this.orgStr,this.curPos,this.curPos)this.curPos = this.curPos +1while (s == "\n" or s=="\r" or s=="\t" or s== "\\" or s == " ") doif this.curPos>=this.len thenreturn ""ends = string.sub(this.orgStr,this.curPos,this.curPos)this.curPos = this.curPos +1endreturn s
endfunction jsonParser.read( )if this.curPos>=this.len thenreturn ""endlocal s = string.sub(this.orgStr,this.curPos,this.curPos)this.curPos = this.curPos +1while (s == "\n" or s=="\r" or s=="\t" or s == "\\") doif this.curPos>=this.len thenreturn ""ends = string.sub(this.orgStr,this.curPos,this.curPos)this.curPos = this.curPos +1endreturn s
endfunction jsonParser.read_str( )if this.curPos>=this.len thenreturn ""endlocal s = string.sub(this.orgStr,this.curPos,this.curPos)this.curPos = this.curPos +1return s
endfunction jsonParser.trim (s) local trimStr = string.gsub(s, "^%s*(.-)%s*$", "%1")return trimStr
end function jsonParser.is_null(str )if str == nil thenreturn trueelseif str =="" thenreturn trueelsereturn falseend
endfunction jsonParser.get_obj()local jd = {}-- jd[type] = "object"this.token = this.read_un_space()while(this.token~="}" and this.curPos<this.len and this.is_null(this.token) == false) doif this.token ~="," thenlocal key = this.get_key()key = this.trim(key)if this.is_null(key) == true thenbreakendlocal v = this.get_value()local numKey = tonumber(key)if numKey~=nil thenjd[numKey] = velsejd[key] = vendendthis.token = this.read_un_space()endreturn jd
endfunction jsonParser.get_array()local list = {}this.token = this.read_un_space()while(this.token~="]" and this.is_null(this.token) == false) doif this.token == "{" thentable.insert(list,this.get_obj())elseif this.token == "[" thentable.insert(list,this.get_array())elseif this.token~="," thenlocal arrJd = this.get_final_value()if arrJd~=nil thentable.insert(list,arrJd)endendthis.token = this.read_un_space()endreturn list
endfunction jsonParser.get_key()local k = ""while(this.token ~=":" and this.token~="}" and this.is_null(this.token)==false) doif this.token~="\"" and this.token~="{" thenk = this.concat(k,this.token)endthis.token = this.read()endreturn k
endfunction jsonParser.get_value()this.token = this.read_un_space()if this.token == "{" thenreturn this.get_obj()elseif this.token == "[" thenreturn this.get_array()elsereturn this.get_final_value()end
endfunction jsonParser.get_final_value()local k = ""local t = this.tokenlocal tl = string.lower(t)if t == "\"" thenlocal addStr = this.get_string()return addStrelseif tl == "t" or tl == "f" thenlocal addStr = this.get_bool()k = this.concat(k,t)k = this.concat(k,addStr)local b = trueif string.lower(k) == "false" thenb = falseendreturn belseif tl == "n" thenlocal addStr = this.get_null()k = this.concat(k,t)k = this.concat(k,addStr)return nilelsek = this.concat(k,t)local addStr = this.get_num()k = this.concat(k,addStr)k = this.trim(k)if string.lower(k) == "null" thenreturn nilendif k == "}" or k == "]" thenthis.curPos = curPos -1return nilendreturn tonumber(k)endreturn nilendfunction jsonParser.get_string( )local k = ""local last = nilthis.token = this.read_str()while(this.token~="\"" and this.is_null(this.token)==false) doif this.token ~= "\\" thenk = this.concat(k,this.token) endthis.token = this.read_str()endif this.token == "}" or this.token == "]" thenthis.curPos = this.curPos -1end--print("---------get_string------",k)return k
endfunction jsonParser.get_bool()local k = ""this.token = this.read()while(this.token~="\"" and this.token~="," and this.token~="}" and this.token~="]" and this.is_null(this.token)==false) dok = this.concat(k,this.token)this.token = this.read() endif this.token == "}" or this.token == "]" thenthis.curPos = this.curPos -1endreturn k
endfunction jsonParser.get_null()local k = ""this.token = this.read()while(this.token~="\"" and this.token~="," and this.token~="}" and this.token~="]" and this.is_null(this.token)==false) dok = this.concat(k,this.token)this.token = this.read() endif this.token == "}" or this.token == "]" thenthis.curPos = this.curPos -1endreturn k
endfunction jsonParser.get_num( )local k = ""this.token = this.read()while(this.token~="\"" and this.token~="," and this.token~="}" and this.token~="]" and this.is_null(this.token)==false) dok = this.concat(k,this.token)this.token = this.read() endif this.token == "}" or this.token == "]" thenthis.curPos = this.curPos -1endreturn k
endfunction jsonParser.encode( obj )this.tab = objreturn this.get_field(obj)
endfunction jsonParser.get_field(obj)if obj == nil thenreturn ""endlocal str = ""local t = type(obj)if t== "number" thenstr = tostring(obj)elseif t=="string" thenstr = "\""str = this.concat(str,tostring(obj))str = this.concat(str,"\"")elseif t=="boolean" or t=="bool" thenstr = string.lower(tostring(obj))elsestr = this.get_obj_str(obj)endreturn str
endfunction jsonParser.get_obj_str( obj )if obj == nil thenreturn "null"endif type(obj) == "function" thenreturn "func"endlocal key_list = {}local v_list = {}for k,v in pairs(obj) dotable.insert(key_list,k)table.insert(v_list,v)endlocal len = #key_listif len <= 0 thenreturn "{}"endlocal str = "{"for i=1,len dolocal k = key_list[i]local v = v_list[i]str = this.concat(str,this.get_key_str(k))str = this.concat(str,":")str = this.concat(str,this.get_field(v))if i<len thenstr = this.concat(str,",")endendstr = this.concat(str,"}")return str
endfunction jsonParser.get_key_str( k )if type(k) == "string" thenlocal str = "\""str = this.concat(str,tostring(k))str = this.concat(str,"\"")return strelsereturn tostring(k)end
endfunction jsonParser.concat(...)local arg = { ... }local resut = table.concat(arg);return resut;
end
这篇关于阿赵Json工具AzhaoJson的Lua版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!