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 result
= string.format("%q", o
)
25 elseif t
== "table" and o
.__index
== o
then
26 result
= string.format("Class: %q", o
.name
)
27 elseif t
== "table" and not (o
.class
and o
.class
.__tostring
) then
30 nestedIndent
= indent
.. " "
34 if nestedIndent
then result
= result
.. "\n" .. nestedIndent
end
37 for k
, v
in pairs(o
) do
41 result
= result
.. ", "
42 if nestedIndent
then result
= result
.. "\n" .. nestedIndent
end
44 if type(k
) == "number" and k
== nextIndex
then
45 nextIndex
= nextIndex
+ 1
47 if type(k
) == "string" and string.find(k
, "^[_%a][_%w]*$") then
50 result
= result
.. "[" .. serialize(k
, new_max_depth
, nil, seen
) .. "]"
52 result
= result
.. " = "
54 result
= result
.. serialize(v
, new_max_depth
, nestedIndent
, seen
)
57 result
= result
.. "\n" .. indent
59 result
= result
.. "}"