Lua - Table Serialization



Serialization refers to converting data into streams of data/characters. Once data is saved into bytes/characters, we can save the bytes/characters to a file or send over network. In this chapter, we'll see how to serialize various types of Lua data and Lua Table.

Serialize Data

Following code showcases the code to serialize the data.

-- Opens a file in write
file = io.open("test.txt", "w")

-- sets the default output file as test.lua
io.output(file)

-- function to serialize an object
function serialize (o)

   local objType = type(o)
   -- if object is of type number
   if objType == "number" then
      -- serialize the number   
      io.write(o)
   -- if object is of type string
   elseif objType == "string" then
   -- serialize the string after sanitizing the same
      io.write(string.format("%q", o))
   elseif objType == "table" then
      -- serialize the table
	  io.write("{\n")
        for k,v in pairs(o) do
          io.write("  ", k, " = ")
          serialize(v)
          io.write(",\n")
        end
        io.write("}\n")
   else
      error(type(o) .. " cannot be serialized")   
   end
end

Deserialize data

Following code showcases the code to deserialize the data.

-- Opens a file in read
file = io.open("test.l", "r")

-- sets the default input file as test.lua
io.input(file)

-- function to deserialize the object
function deserialize(){
  local data = io.read()
  return data
}

Example - Serialize a Table

main.lua

-- Opens a file in read
file = io.open("test.txt", "w")

-- sets the default output file as test.lua
io.output(file)

-- function to serialize an object
function serialize (o)

   local objType = type(o)
   -- if object is of type number
   if objType == "number" then
      -- serialize the number   
      io.write(o)
   -- if object is of type string
   elseif objType == "string" then
   -- serialize the string after sanitizing the same
      io.write(string.format("%q", o))
   elseif objType == "table" then
      -- serialize the table
        for k,v in pairs(o) do          
          serialize(v) 
          io.write(",")
        end        
   else
      error(type(o) .. " cannot be serialized")   
   end
end

days = { "Monday","Tuesday", "Wednesday","Thursday","Friday","Saturday","Sunday" }

serialize(days)

Output

Now open test.txt to verify the following output:

"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday",

Example - Deserialize a Table

main.lua

-- Opens a file in read
file = io.open("test.txt", "r")

-- sets the default input file as test.lua
io.input(file)

days = {}

input = io.read()

-- for each line
for entry in string.gmatch(input, "([^,]+)") do
   -- add the entry to days
   table.insert(days, entry)
end

-- print table entries
for _,day in ipairs(days) do
   print(day)
end

Output

When the above code is built and executed, it produces the following result −

"Monday"
"Tuesday"
"Wednesday"
"Thursday"
"Friday"
"Saturday"
"Sunday"
Advertisements