Skip to content

Instantly share code, notes, and snippets.

@Redwoodtj
Forked from Humbedooh/quickjson.lua
Created January 8, 2023 10:20
Show Gist options
  • Save Redwoodtj/55aa58b6ce66af2769fb772be78536ca to your computer and use it in GitHub Desktop.
Save Redwoodtj/55aa58b6ce66af2769fb772be78536ca to your computer and use it in GitHub Desktop.
-- quick and dirty JSON conversion
local function quickJSON(input)
if type(input) == "table" then
local t = 'array'
for k, v in pairs(input) do
if type(k) ~= "number" then
t = 'hash'
break
end
end
if t == 'hash' then
local tbl = {}
for k, v in pairs(input) do
local kv = ([["%s": %s]]):format(k, quickJSON(v))
table.insert(tbl, kv)
end
return "{" .. table.concat(tbl, ", ") .. "}"
else
local tbl = {}
for k, v in pairs(input) do
table.insert(tbl, quickJSON(v))
end
return "[" .. table.concat(tbl, ", ") .. "]"
end
elseif type(input) == "string" then
return ([["%s"]]):format(input:gsub('"', '\\"'):gsub("[\r\n\t]", " "))
elseif type(input) == "number" then
return tostring(input)
elseif type(input) == "boolean" then
return (input and "true" or "false")
else
return "null"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment