Lua - List 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 List.
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))
else
-- iterate throgh entries
for v in o:iterator() do
serialize(v[1])
io.write(",")
end
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 List
Following code showcases the code to serialize the data.
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))
else
-- iterate throgh entries
for v in o:iterator() do
serialize(v[1])
io.write(",")
end
end
end
-- List Implementation
list = {}
list.__index = list
setmetatable(list, { __call = function(_, ...)
local t = setmetatable({ length = 0 }, list)
for _, v in ipairs{...}
do t:push(v)
end
return t
end })
-- push an element to the end of the list
function list:push(t)
-- move till last node
if self.last then
self.last._next = t
t._prev = self.last
self.last = t
else
-- set the node as first node
self.first = t
self.last = t
end
-- increment the length of the list
self.length = self.length + 1
end
-- iterate through the list
local function iterate(self, current)
if not current then
current = self.first
elseif current then
current = current._next
end
return current
end
function list:iterator()
return iterate, self, nil
end
days = list({"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 List
Following code showcases the code to deserialize the data.
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
days:push({entry})
end
-- iterate throgh entries
for v in days:iterator() do
print(v[1])
end
Output
When we run the above code, we will get the following output−
"Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
Advertisements