59 lines
1.5 KiB
Lua
59 lines
1.5 KiB
Lua
-- Minimal JSON encoder for AscensionExporter
|
|
|
|
local function escape_str(s)
|
|
s = tostring(s)
|
|
s = s:gsub('\\', '\\\\')
|
|
s = s:gsub('"', '\\"')
|
|
s = s:gsub('\n', '\\n')
|
|
s = s:gsub('\r', '\\r')
|
|
s = s:gsub('\t', '\\t')
|
|
return '"' .. s .. '"'
|
|
end
|
|
|
|
local function is_array(t)
|
|
if type(t) ~= 'table' then return false end
|
|
local n = 0
|
|
for k, _ in pairs(t) do
|
|
if type(k) ~= 'number' then return false end
|
|
n = n + 1
|
|
end
|
|
for i = 1, n do
|
|
if t[i] == nil then return false end
|
|
end
|
|
return true
|
|
end
|
|
|
|
local function encode_value(v)
|
|
local tv = type(v)
|
|
if tv == 'nil' then
|
|
return 'null'
|
|
elseif tv == 'boolean' then
|
|
return v and 'true' or 'false'
|
|
elseif tv == 'number' then
|
|
return tostring(v)
|
|
elseif tv == 'string' then
|
|
return escape_str(v)
|
|
elseif tv == 'table' then
|
|
if is_array(v) then
|
|
local parts = {}
|
|
for i = 1, #v do
|
|
parts[#parts+1] = encode_value(v[i])
|
|
end
|
|
return '[' .. table.concat(parts, ',') .. ']'
|
|
else
|
|
local parts = {}
|
|
for k, val in pairs(v) do
|
|
local key = escape_str(k)
|
|
parts[#parts+1] = key .. ':' .. encode_value(val)
|
|
end
|
|
return '{' .. table.concat(parts, ',') .. '}'
|
|
end
|
|
else
|
|
return 'null'
|
|
end
|
|
end
|
|
|
|
function AscensionExporter_Json_Encode(obj)
|
|
return encode_value(obj)
|
|
end
|