用于序列化lua表的方法

cct*_*tan 7 lua serialization coronasdk lua-table

我可能错过了这个,但有没有内置的方法将lua表序列化/反序列化为文本文件,反之亦然?

我有一对方法来在具有固定格式的lua表上执行此操作(例如,3列数据,5行).

有没有办法做到这一点与任何LUA表任意格式?

举个例子,给出这个lua表:

local scenes={
    {name="scnSplash",
        obj={
            {
                name="bg",
                type="background",
                path="scnSplash_bg.png",
            },
            {
                name="bird",
                type="image",
                path="scnSplash_bird.png",
                x=0, 
                y=682,
            },
        }
    },
}
Run Code Online (Sandbox Code Playgroud)

它将被转换为这样的文本:

{name="scnSplash",obj={{name="bg",type="background",path="scnSplash_bg.png",},{name="bird",  type="image",path="scnSplash_bird.png",x=0,y=682,}},}
Run Code Online (Sandbox Code Playgroud)

只要文本字符串可以反序列化为空的lua表,就可以以任何方式定义序列化文本的格式.

Pau*_*nko 8

我不确定为什么JSON库被标记为正确答案,因为它似乎在序列化"任意格式的lua表"时非常有限.它不将boolean/table/function值作为键处理,也不处理循环引用.共享引用未序列化为共享,并且math.huge在Windows上未正确序列化值.我意识到其中大部分都是JSON限制(因此在库中以这种方式实现),但这被提议作为通用Lua表序列化的解决方案(事实并非如此).

通过使用TableSerialization页面或我的Serpent序列化器和漂亮打印机中的一个实现,可以更好.


Man*_*agu 5

Lua 本身没有任何这样的内置函数,但实现一个并不困难。这里列出了许多预烘焙的实现:http : //lua-users.org/wiki/TableSerialization


cct*_*tan 2

require "json"
local t = json.decode( jsonFile( "sample.json" ) )
Run Code Online (Sandbox Code Playgroud)

参考这里一个简单的 json 序列化器。