Created
May 9, 2017 09:56
-
-
Save Humbedooh/2845b02182df5ebb83a155f8f8f616cd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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