阿赵Json工具AzhaoJson的Lua版本

devtools/2024/9/23 17:24:49/

  大家好,我是阿赵。
  之前分享了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

http://www.ppmy.cn/devtools/22381.html

相关文章

WebStorm使用CNPM

简介 1、Node.js是一个基于Chrome V8引擎的JavaScript运行环境&#xff0c; 2、cnpm是淘宝NPM镜像 3、WebStorm则是一款功能强大的JavaScript集成开发环境&#xff08;IDE&#xff09; 安装Node.js和cnpm 安装Node.js&#xff1a;首先&#xff0c;你需要从Node.js官方网站下…

iOS 64位程序调试环境搭建

一、背景 调试ios程序经常使用gdb&#xff0c;但是gdb还未支持arm64&#xff0c;需要使用XCode的lldb调试IOS下64位程序。因为xcode默认调试工具是lldb&#xff0c;开始各种尝试在xcode中使用Debug-》Attach to Process 直接attach进程&#xff0c;但是只能看见进程却无法挂接。…

Python网络爬虫项目开发实战:怎么解决数据抓取

注意&#xff1a;本文的下载教程&#xff0c;与以下文章的思路有相同点&#xff0c;也有不同点&#xff0c;最终目标只是让读者从多维度去熟练掌握本知识点。 下载教程&#xff1a;Python网络爬虫项目开发实战_数据抓取_编程案例解析实例详解课程教程.pdf 数据抓取&#xff0c;…

Git -- 运用总结

文章目录 1. Git2. 基础/查阅2.1 基础/查阅 - git2.2 仓库 - remote2.3 清理 - rm/clean2.4 版本回退 - reset 3. 分支3.1 分支基础 - branch3.2 分支暂存更改 - stash3.3 分支切换 - checkout 4. 代码提交/拉取4.1 代码提交 - push4.2 代码拉取 - pull 1. Git 2. 基础/查阅 2…

小白学习SpringCloud之Eureka

前言 需要搭建springcloud项目&#xff0c;eureka是其中的一个模块&#xff0c;依赖主要继承父依赖 学习视频&#xff1a;b站狂神说 便于理解,我修改了本地域名》这里!!! 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.comEureka入门案例 eureka…

AI-数学-高中-43常见函数的导数

原作者视频&#xff1a;【导数】【一数辞典】2常见函数的导数_哔哩哔哩_bilibili

个人学习总结__打开摄像头、播放网络视频的以及ffmpeg推流

前言 最近入手了一款非常便宜的usb摄像头&#xff08;买回来感觉画质很低&#xff0c;没有描述的4k&#xff0c;不过也够用于学习了&#xff09;,想着利用它来开启流媒体相关技术的学习。第一步便是打开摄像头&#xff0c;从而才能够对它进行一系列后续操作&#xff0c;诸如实…

复旦大学-华盛顿大学EMBA项目 “领导力”试听课

复旦大学-华盛顿大学EMBA项目&#xff0c;是由美国华盛顿大学奥林商学院与复旦大学管理学院联合开办的EMBA项目。英国《金融时报》&#xff08;FT&#xff09;发布2022年全球EMBA排名&#xff0c;复旦大学-华盛顿大学EMBA项目位列全球第9位&#xff0c;学术研究连续3年蝉联亚洲…