lua表序列化的方法 [英] method for serializing lua tables

查看:194
本文介绍了lua表序列化的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能错过了这一点,但是是否有一种内置方法可以将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆