序列化lua表的方法 [英] method for serializing lua tables
问题描述
我可能错过了这个,但是有没有内置的方法可以将 lua 表序列化/反序列化到文本文件,反之亦然?
I may have missed this, but is there a built-in method for serializing/deserializing lua tables to text files and vice versa?
我有一对方法可以在固定格式的 lua 表上执行此操作(例如,3 列数据和 5 行).
I had a pair of methods in place to do this on a lua table with fixed format (e.g. 3 columns of data with 5 rows).
有没有办法在具有任何任意格式的lua表上执行此操作?
Is there a way to do this on lua tables with any arbitrary format?
举个例子,给定这个lua表:
For an example, given this lua table:
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,
},
}
},
}
它会被转换成这样的文本:
It would be converted into text like this:
{name="scnSplash",obj={{name="bg",type="background",path="scnSplash_bg.png",},{name="bird", type="image",path="scnSplash_bird.png",x=0,y=682,}},}
序列化文本的格式可以任意定义,只要可以将文本字符串反序列化为空的lua表即可.
The format of the serialized text can be defined in any way, as long as the text string can be deserialized into an empty lua table.
推荐答案
require "json"
local t = json.decode( jsonFile( "sample.json" ) )
参考 此处 用于简单的 json 序列化程序.
reference here for a simple json serializer.
这篇关于序列化lua表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!