forked from jahands/FactorioMods_FactorioMaps
-
Notifications
You must be signed in to change notification settings - Fork 22
/
json.lua
33 lines (33 loc) · 928 Bytes
/
json.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
function prettyjson(o, i)
local tab = '\n'..string.rep('\t', i or 0)
if type(o) == 'table' then
local s = o[1] and '[' or '{'
for k,v in pairs(o) do
s = s..tab..(o[1] and '\t' or '\t"'..k..'": ')..json(v, 1+(i or 0))..','
end
return s:sub(1, -2)..(#s>1 and tab..(o[1] and ']' or '}') or '[]')
end
if type(o) == 'number' then
return tostring(o)
elseif type(o) == 'boolean' then
return o and "true" or "false"
else
return '"'..tostring(o):gsub('"', '\\"')..'"'
end
end
function json(o, i)
if type(o) == 'table' then
local s = o[1] and '[' or '{'
for k,v in pairs(o) do
s = s .. (o[1] and '' or '"'..k..'":')..json(v, 1+(i or 0))..','
end
return s:sub(1, -2)..(#s>1 and (o[1] and ']' or '}') or '[]')
end
if type(o) == 'number' then
return tostring(o)
elseif type(o) == 'boolean' then
return o and "true" or "false"
else
return '"'..tostring(o):gsub('"', '\\"')..'"'
end
end