-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAPI-JSONdumper.lua
59 lines (48 loc) · 1.46 KB
/
API-JSONdumper.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
--Initial call
--dumpObjectFunc( _G, '_G', 0 )
--Only outputs tables and functions!
function dumpObjectFunc( obj, name, indent )
--build our indentation offset
local indentStr = '';
for i=1,indent do
indentStr = indentStr..' '
end
--keep track of what we did to prevent infinite cycles
doneObjs[obj] = true
--open the JSON object
print(indentStr..'"'..name..'" : ')
print(indentStr.."{")
--loop over all fields
for k,v in pairsByKeys(obj) do
--if the field is a table recurse
if type(v) == 'table' and k ~= 'FDesc' and doneObjs[v] == nil and type(k) ~= 'table' and k ~= 'doneObjs' then
dumpObjectFunc( v, k, indent + 1)
elseif type(v) == 'function' then
--if the field is a function try to find a description and print
if obj.FDesc and obj.FDesc[k] then
print(indentStr..' "'..k..'" : '..'"'..string.gsub(tostring(obj.FDesc[k]), '\n', '\\n ')..'",')
else
print(indentStr..' "'..k..'" : '..'"No description",')
end
elseif type(v) == 'number' or type(v) == 'string' then
--print constants
print(indentStr..' "'..k..'" : "'..v..'",')
end
end
print('"##FIX##"')
--close JSON object
print(indentStr.."},")
end
function pairsByKeys (t, f)
local a = {}
for n in pairs(t) do table.insert(a, n) end
table.sort(a, f)
local i = 0 -- iterator variable
local iter = function () -- iterator function
i = i + 1
if a[i] == nil then return nil
else return a[i], t[a[i]]
end
end
return iter
end