2 require
"data_structures"
4 function serialize(o
, maxdepth
, indent
, s
)
5 local seen
= s
or Stack
:new()
8 if maxdepth
then new_max_depth
= maxdepth
-1 end
9 if new_max_depth
== 0 then return "<too deep>" end
11 if t
== "table" and seen
:contains(o
) then
17 if indent
and type(indent
) ~= "string" then
24 nestedIndent
= indent
.. " "
28 result
= string.format("%q", o
)
29 elseif t
== "boolean" or t
== "number" then
31 elseif t
== "table" and o
.__index
== o
and o
.name
then
32 result
= string.format("Class: %q", o
.name
)
33 elseif t
== "table" and not (o
.class
and o
.class
.tostring) then
35 if nestedIndent
then result
= result
.. "\n" .. nestedIndent
end
38 for k
, v
in pairs(o
) do
42 result
= result
.. ", "
43 if nestedIndent
then result
= result
.. "\n" .. nestedIndent
end
45 if type(k
) == "number" and k
== nextIndex
then
46 nextIndex
= nextIndex
+ 1
48 if type(k
) == "string" and string.find(k
, "^[_%a][_%w]*$") then
51 result
= result
.. "[" .. serialize(k
, new_max_depth
, nil, seen
) .. "]"
53 result
= result
.. " = "
55 result
= result
.. serialize(v
, new_max_depth
, nestedIndent
, seen
)
58 result
= result
.. "\n" .. indent
60 result
= result
.. "}"
62 result
= o
:tostring(nestedIndent
)