-
Notifications
You must be signed in to change notification settings - Fork 11
/
sh_tableserialize.lua
45 lines (39 loc) · 1.36 KB
/
sh_tableserialize.lua
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
-- Example available on: https://glot.io/snippets/fqw1us9t1q
-- Converts lua table to raw text.
-- Made for printing tables to the console on other runtime environments than the glua.
-- It can be also used as json alt.
local strrepeat, pairs, type, format, tostring, print = string.rep, pairs, type, string.format, tostring, print
function table.ToPlain(tbl, indent, out)
local isend = not out
out = out or "{\n"
indent = indent or 1
for k, v in pairs(tbl) do
local tabs = strrepeat(" ", indent)
formatting = tabs .. k .." = "
if type(v) == "table" then
out = out .. formatting .."{\n"
out = out .. table.ToPlain(v, indent + 1, "")
out = out .. tabs .."},\n"
else
out = out .. formatting .. (type(v) == "string" and format("%q", v) or tostring(v)) ..",\n"
end
end
if isend then out = out .."}\n" end
return out
end
function table.Print(tbl, indent)
indent = indent or 0
for k, v in pairs(tbl) do
formatting = strrepeat(" ", indent) .. k ..": "
if type(v) == "table" then
print(formatting)
table.Print(v, indent + 1)
else
print(formatting .. tostring(v))
end
end
end
if PrintTable then return end
function PrintTable(t) -- GLua like but on pure lua
print(table.ToPlain(t))
end