Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

method for serializing lua tables

I may have missed this, but is there a built-in method for serializing/deserializing lua tables to text files and vice versa?

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).

Is there a way to do this on lua tables with any arbitrary format?

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

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.

like image 590
cctan Avatar asked Jan 31 '12 04:01

cctan


People also ask

What is the use of serialize method?

The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

What is serializing in code?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is serializing content?

Content serialization involves taking one concept or topic and turning it into many smaller chunks or installments, so you develop a series based on one idea. For example, the Lord of the Rings trilogy was too large for one story, so Tolkien broke it down into three parts.

What is serializing a string?

String serialization is the process of writing a state of object into a byte stream. In python, the “pickle” library is used for enabling serialization. This module includes a powerful algorithm for serializing and de-serializing a Python object structure.


4 Answers

Lua alone doesn't have any such builtin, but implementing one is not difficult. A number of prebaked implementations are listed here: http://lua-users.org/wiki/TableSerialization

like image 150
Managu Avatar answered Nov 02 '22 15:11

Managu


I'm not sure why JSON library was marked as the right answer as it seems to be very limited in serializing "lua tables with any arbitrary format". It doesn't handle boolean/table/function values as keys and doesn't handle circular references. Shared references are not serialized as shared and math.huge values are not serialized correctly on Windows. I realize that most of these are JSON limitations (and hence implemented this way in the library), but this was proposed as a solution for generic Lua table serialization (which it is not).

One would be better off by using one of the implementations from TableSerialization page or my Serpent serializer and pretty-printer.

like image 10
Paul Kulchenko Avatar answered Nov 02 '22 17:11

Paul Kulchenko


require "json"
local t = json.decode( jsonFile( "sample.json" ) )

reference here for a simple json serializer.

like image 2
cctan Avatar answered Nov 02 '22 15:11

cctan


Add json.lua from rxi/json.lua to your project, then use it with:

local json = require("json")

local encoded = json.encode({
  name = "J. Doe",
  age = 42
})

local decoded = json.decode(encoded)

print(decoded.name)

Note that the code chokes if there are functions in the value you are trying to serialize. You have to fix line 82 and 93 in the code to skip values that have the function type.

like image 1
mhvelplund Avatar answered Nov 02 '22 17:11

mhvelplund