首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存储为string的简单Lua表的反序列化

存储为string的简单Lua表的反序列化
EN

Stack Overflow用户
提问于 2018-08-06 06:22:56
回答 1查看 617关注 0票数 3

我正在将一个字符串中的lua表文字从一个web应用程序传输到PICO-8,我正在尝试反序列化返回到PICO-8中的lua表。

字符串以'{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'形式存在。

为了保持简单,我只打算在字符串中包含小写字符,嵌套表中只允许字符串。

我觉得我已经掌握了如何解析字符,但我不知道如何跟踪重新创建的数据中的位置,包括结构的深度和索引。

这通常是怎么做到的?

问题是,由于Pico-8Lua不包含loadloadstring,所以解析必须手动进行。下面的代码使用的是table.insertstring.sub,而不是PICO-8等价物,因为我使用lua来帮助原型这段代码。

这是到目前为止我对打印声明的看法,我认为我需要在哪里做。

任何帮助都将不胜感激。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 11:41:18

使用Lua模式避免解析每个字符。

代码语言:javascript
复制
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) -- arrays
   s = s:gsub('"(.-)"', save)                                                   -- strings
   s = s:gsub('%-?%d+', function(s) return save(tonumber(s)) end)               -- integer numbers
   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.subtable.inserttonumbertype等函数

代码语言:javascript
复制
local 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"}}}

字符串必须用"括起来,不能在其中包含字符\。字符串可能是空的。

数字必须是带有可选减号的十进制表示法中的整数。

数组只能包含字符串、数字和子数组。数组可能为空。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51701629

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档