阿赵Json工具AzhaoJson的Lua版本

2024-04-29 05:44

本文主要是介绍阿赵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版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/945240

相关文章

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

C#下Newtonsoft.Json的具体使用

《C#下Newtonsoft.Json的具体使用》Newtonsoft.Json是一个非常流行的C#JSON序列化和反序列化库,它可以方便地将C#对象转换为JSON格式,或者将JSON数据解析为C#对... 目录安装 Newtonsoft.json基本用法1. 序列化 C# 对象为 JSON2. 反序列化

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Ubuntu如何升级Python版本

《Ubuntu如何升级Python版本》Ubuntu22.04Docker中,安装Python3.11后,使用update-alternatives设置为默认版本,最后用python3-V验证... 目China编程录问题描述前提环境解决方法总结问题描述Ubuntu22.04系统自带python3.10,想升级

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

MySQL慢查询工具的使用小结

《MySQL慢查询工具的使用小结》使用MySQL的慢查询工具可以帮助开发者识别和优化性能不佳的SQL查询,本文就来介绍一下MySQL的慢查询工具,具有一定的参考价值,感兴趣的可以了解一下... 目录一、启用慢查询日志1.1 编辑mysql配置文件1.2 重启MySQL服务二、配置动态参数(可选)三、分析慢查

更改linux系统的默认Python版本方式

《更改linux系统的默认Python版本方式》通过删除原Python软链接并创建指向python3.6的新链接,可切换系统默认Python版本,需注意版本冲突、环境混乱及维护问题,建议使用pyenv... 目录更改系统的默认python版本软链接软链接的特点创建软链接的命令使用场景注意事项总结更改系统的默

Linux升级或者切换python版本实现方式

《Linux升级或者切换python版本实现方式》本文介绍在Ubuntu/Debian系统升级Python至3.11或更高版本的方法,通过查看版本列表并选择新版本进行全局修改,需注意自动与手动模式的选... 目录升级系统python版本 (适用于全局修改)对于Ubuntu/Debian系统安装后,验证Pyt