将Lua简单表格存储为字符串反序列化
2018-8-6 15:45:30
收藏:0
阅读:80
评论:1
我正在将一个包含Lua表格文本的字符串从网络应用程序传输到 PICO-8,试图将其反序列化为 PICO-8 中的Lua表格。
该字符串的格式为 '{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'
为了保持简单,我只打算在字符串中使用小写字符,并且只允许嵌套表格中存在字符串。
我感觉我已经理解如何解析这些字符,但我不知道如何跟踪我在重新创建的数据中的位置,包括结构深度和索引。
通常要如何处理这个问题?
问题在于 PICO-8 Lua 不包含 load 或 loadstring,因此必须手动进行解析。以下代码使用 table.insert 和 string.sub 代替 PICO-8 的等效函数,因为我在使用 Lua REPL 原型化此代码。
这是我到目前为止的代码,其中包含所需的打印语句。
如果能得到帮助,将不胜感激。
test_obj = {"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}
data_string = '{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'
data = nil
string = ''
level = 0
while #data_string > 0 do
local d=string.sub(data_string,1,1)
if stringChar(d) then
string = string..d
end
if comma(d) then
print(string)
table.insert(data, string)
string = ''
end
if openBracket(d) then
if data == nil then
data = {}
print('new table')
else
print('insert table')
end
level = level + 1
print('on level', level)
end
if closeBracket(d) then
print('end of table')
level = level - 1
print('up to level', level)
end
data_string=string.sub(data_string,2)
end
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

使用 Lua 模式来避免每个字符的解析
local function read_exp_list(s) local exps, res = {}, {} local function save(v) exps[#exps + 1] = v return ('\0'):rep(#exps) end s = s:gsub('%b{}', function(s) return save{read_exp_list(s:sub(2, -2))} end) -- 数组 s = s:gsub('"(.-)"', save) -- 字符串 s = s:gsub('%-?%d+', function(s) return save(tonumber(s)) end) -- 整数 for k in s:gmatch'%z+' do res[#res + 1] = exps[#k] end return (table.unpack or unpack)(res) end local data_string = '{-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}' local obj = read_exp_list(data_string) -- obj == {-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}字符串必须用
"括起来,不能包含字符{}\。字符串可以是空的。数字必须是十进制表示的整数,可选带负号。
数组必须包含只有字符串、数字和子数组。数组可以是空的。
更新:
以下代码仅使用函数
string.sub、table.insert、tonumber、typelocal function is_digit(c) return c >= '0' and c <= '9' end local function read_from_string(input) if type(input) == 'string' then local data = input local pos = 0 function input(undo) if undo then pos = pos - 1 else pos = pos + 1 return string.sub(data, pos, pos) end end end local c repeat c = input() until c ~= ' ' and c ~= ',' if c == '"' then local s = '' repeat c = input() if c == '"' then return s end s = s..c until c == '' elseif c == '-' or is_digit(c) then local s = c repeat c = input() local d = is_digit(c) if d then s = s..c end until not d input(true) return tonumber(s) elseif c == '{' then local arr = {} local elem repeat elem = read_from_string(input) table.insert(arr, elem) until not elem return arr end end local data_string = '{-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}' local obj = read_from_string(data_string) -- obj == {-42, "top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}字符串必须用
"括起来,不能包含字符\。字符串可以是空的。数字必须是十进制表示的整数,可选带负号。
数组必须包含只有字符串、数字和子数组。数组可以是空的。