简单 Lua table 存储为字符串的反序列化

Deserialization of simple Lua table stored as string

我正在将字符串中的 lua table 文字从 Web 应用程序传输到 PICO-8,我试图将其反序列化回 lua table 在 PICO-8 中。

字符串的形式是'{"top", {"one", {"one a", "one b"}}, {"two", {"two a", "two b"}}}'

为了尽量保持简单,我只打算在字符串中包含小写字符,并且在嵌套的 table 中只允许使用字符串。

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

这通常是如何完成的?

要注意的是,由于 PICO-8 lua 不包含 loadloadstring,因此必须手动进行解析。以下代码使用 table.insertstring.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 模式来避免解析每个字符

local function read_exp_list(s)
   local exps, res = {}, {}
   local function save(v)
      exps[#exps + 1] = v
      return ('[=10=]'):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

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"}}}

字符串必须用"括起来,里面不能有字符\。字符串可能为空。
数字必须是带可选减号的十进制整数。
数组必须只包含字符串、数字和子数组。数组可能为空。