Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function DecodeJSON(input)
- -- This function tries to convert the syntax of a JSON table into a Lua Table,
- -- and dumps it as a table using loadstring.
- local str = "return "
- while #input > 0 do
- local chunk = string.sub(input,1,1)
- input = string.sub(input,2)
- if chunk == [["]] then
- local quoteEnd = string.find(input,[["]])
- if not quoteEnd then
- print(input)
- error()
- end
- local key = string.sub(input,1,quoteEnd-1)
- input = string.sub(input,#key+2)
- if not tonumber(key) then
- -- See if we could use the string alone as a key for a table without brackets or quotes
- -- If not, then we need to put quotes around it.
- local noQuotes = pcall(function ()
- assert(loadstring("t = {" .. key .." = 0}"))
- end)
- if not noQuotes then
- key = [["]] .. key .. [["]]
- end
- end
- str = str .. key
- elseif chunk == "[" then
- str = str .. "{"
- elseif chunk == "]" then
- str = str .. "}"
- elseif chunk == ":" then
- str = str .. " = "
- else
- str = str .. chunk
- end
- end
- return loadstring(str)()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement