-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex15.4.lua
More file actions
61 lines (54 loc) · 1.4 KB
/
ex15.4.lua
File metadata and controls
61 lines (54 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
local function is_list(t)
assert(type(t) == "table")
local last
for k, _ in pairs(t) do
if type(k) ~= "number" then
return false
end
if last and k ~= last + 1 then
return false
end
last = tonumber(k)
end
return true
end
local function get_serialize(s)
if type(s) == "number" then
return string.format("[%s]", s)
end
if type(s) == "string" and not string.match(s, "[%a_][%a%d_]*") then
return string.format("[%q]", s)
end
return s
end
local function serialize (o, indent)
indent = indent or 0
local t = type(o)
if t == "number" or t == "string" or t == "boolean" or
t == "nil" then
io.write(string.format("%q", o))
elseif t == "table" then
io.write("\n", string.rep(" ", indent), "{\n")
indent = indent + 2
if not is_list(o) then
for k,v in pairs(o) do
io.write(string.rep(" ", indent), string.format("%s = ", get_serialize(k)))
serialize(v, indent)
io.write(",\n")
end
else
for _, v in pairs(o) do
serialize(v, indent)
io.write(",\n")
end
end
indent = indent - 2
io.write(string.rep(" ", indent), "}\n")
else
error("cannot serialize a " .. type(o))
end
end
local q = { [1] = 1, [2] = 2, [4] = 4 }
local s = { ["1@#"] = 10, y = 20, z = 30, a = q }
local t = { 1, 2, 3, s }
serialize(t)