Skip to content

Instantly share code, notes, and snippets.

@Humbedooh
Created May 9, 2017 09:56
Show Gist options
  • Save Humbedooh/2845b02182df5ebb83a155f8f8f616cd to your computer and use it in GitHub Desktop.
Save Humbedooh/2845b02182df5ebb83a155f8f8f616cd 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