11 local tostring = tostring; |
11 local tostring = tostring; |
12 local t_insert = table.insert; |
12 local t_insert = table.insert; |
13 local t_concat = table.concat; |
13 local t_concat = table.concat; |
14 local error = error; |
14 local error = error; |
15 local pairs = pairs; |
15 local pairs = pairs; |
|
16 local next = next; |
16 |
17 |
17 local debug_traceback = debug.traceback; |
18 local debug_traceback = debug.traceback; |
18 local log = require "util.logger".init("serialization"); |
19 local log = require "util.logger".init("serialization"); |
19 module "serialization" |
20 module "serialization" |
20 |
21 |
32 if type(o) == "number" then |
33 if type(o) == "number" then |
33 func(t, tostring(o)); |
34 func(t, tostring(o)); |
34 elseif type(o) == "string" then |
35 elseif type(o) == "string" then |
35 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); |
36 func(t, (("%q"):format(o):gsub("\\\n", "\\n"))); |
36 elseif type(o) == "table" then |
37 elseif type(o) == "table" then |
37 func(t, "{\n"); |
38 if next(o) then |
38 for k,v in pairs(o) do |
39 func(t, "{\n"); |
39 func(t, indent(ind)); |
40 for k,v in pairs(o) do |
40 func(t, "["); |
41 func(t, indent(ind)); |
41 func(t, basicSerialize(k)); |
42 func(t, "["); |
42 func(t, "] = "); |
43 func(t, basicSerialize(k)); |
43 if ind == 0 then |
44 func(t, "] = "); |
44 _simplesave(v, 0, t, func); |
45 if ind == 0 then |
45 else |
46 _simplesave(v, 0, t, func); |
46 _simplesave(v, ind+1, t, func); |
47 else |
|
48 _simplesave(v, ind+1, t, func); |
|
49 end |
|
50 func(t, ",\n"); |
47 end |
51 end |
48 func(t, ",\n"); |
52 func(t, indent(ind-1)); |
|
53 func(t, "}"); |
|
54 else |
|
55 func(t, "{}"); |
49 end |
56 end |
50 func(t, indent(ind-1)); |
|
51 func(t, "}"); |
|
52 elseif type(o) == "boolean" then |
57 elseif type(o) == "boolean" then |
53 func(t, (o and "true" or "false")); |
58 func(t, (o and "true" or "false")); |
54 else |
59 else |
55 log("error", "cannot serialize a %s: %s", type(o), debug_traceback()) |
60 log("error", "cannot serialize a %s: %s", type(o), debug_traceback()) |
56 func(t, "nil"); |
61 func(t, "nil"); |