method for serializing lua tables
Asked Answered
S

5

9

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.

Sverige answered 31/1, 2012 at 4:9 Comment(2)
This question provides some useful info on serialization formats: #6355997Nonparticipating
This looks like a job for Jsonman!Britney
S
1
require "json"
local t = json.decode( jsonFile( "sample.json" ) )

reference here for a simple json serializer.

Sverige answered 31/1, 2012 at 5:44 Comment(0)
E
13

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.

Etherify answered 13/6, 2012 at 19:14 Comment(1)
I agree with you, Lua is not JS.Magdalenemagdalenian
B
5

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

Buatti answered 31/1, 2012 at 4:20 Comment(1)
ouch, no wonder lua tables look like json, there was already a library for Corona in here.Sverige
S
1
require "json"
local t = json.decode( jsonFile( "sample.json" ) )

reference here for a simple json serializer.

Sverige answered 31/1, 2012 at 5:44 Comment(0)
B
0

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.

Britney answered 27/9, 2020 at 10:3 Comment(0)
D
0

Small solution: The key can be done without brackets, but be sure that here is no minuses or other special symbols.

local nl = string.char(10) -- newline
function serialize_list (tabl, indent)
    indent = indent and (indent.."  ") or ""
    local str = ''
    str = str .. indent.."{"..nl
    for key, value in pairs (tabl) do
        local pr = (type(key)=="string") and ('["'..key..'"]=') or ""
        if type (value) == "table" then
            str = str..pr..serialize_list (value, indent)
        elseif type (value) == "string" then
            str = str..indent..pr..'"'..tostring(value)..'",'..nl
        else
            str = str..indent..pr..tostring(value)..','..nl
        end
    end
    str = str .. indent.."},"..nl
    return str
end

local str = serialize_list(tables)
print(str)
Darius answered 8/1, 2021 at 9:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.