-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtbl_serialize.lua
109 lines (91 loc) · 2.06 KB
/
tbl_serialize.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---serialize lua table to string
--@param {table} tbl
-- input table
--
--@returns {string}
-- serialized table
function tbl_serialize(tbl)
local function encode_value(value)
--ascii unit separator
--delimits number
local value_token = "\31"
--ascii acknowledge
--delimits true boolean
local bool_true = "\6"
--ascii negative acknowledge
--delimits false boolean
local bool_false = "\21"
--ascii start of text
--delimits start of string
local str_token = "\2"
--end of string sequence
--ascii end of text + x
--ascii end of transmission block
local str_end_token = "\3x\23"
--asci group separator
--delimits start of table
local tbl_token = "\29"
if type(value) == "boolean" then
return value and bool_true or bool_false
elseif type(value) == "string" then
--check for
--ascii control chars
for i = 1, #value do
if ord(sub(value, i, i)) < 32 then
--delimit binary string
return str_token .. value .. str_end_token
end
end
--encode regular string
return value_token .. value
elseif type(value) == "table" then
return tbl_token .. tbl_serialize(value)
end
return value_token .. value
end
local str = ""
--ascii record separator
--delimits table key
local key_token = "\30"
--ascii end of medium
--delimits end of table
local tbl_end_token = "\25"
--get indexed values
for _, v in ipairs(tbl) do
str = str .. encode_value(v)
end
--get keyed values
for k, v in pairs(tbl) do
if type(k) ~= "number" then
str = str .. key_token .. k .. encode_value(v)
end
end
str = str .. tbl_end_token
return str
end
---validate output
--@usage
-- output = tbl_serialize(my_table)
--
-- tbl_serialize_validate(
-- my_table,
-- tbl_deserialize(output)
-- )
--
--@param {table} tbl1
-- input table
--
--@param {table} tbl2
-- output table
function tbl_serialize_validate(tbl1, tbl2)
for k, v in pairs(tbl1) do
if type(v) == "table" then
tbl_serialize_validate(tbl2[k], v)
else
assert(
tbl2[k] == v,
tostr(k) .. ": " .. tostr(v) .. ": " .. tostr(tbl2[k])
)
end
end
end