Skip to content

Instantly share code, notes, and snippets.

@hosaka
Created March 17, 2015 02:04
Show Gist options
  • Save hosaka/64cb5469c7323fc92ccc to your computer and use it in GitHub Desktop.
Save hosaka/64cb5469c7323fc92ccc to your computer and use it in GitHub Desktop.
very crude but minimal JSON encode/decode in LUA
-- json parsing
function json_decode(msg)
local js = {}
for k,v in string.gmatch(msg,'"(%w+)":"(%w+)",') do
js[k] = v;
end
for k,v in string.gmatch(msg,'"(%w+)":"(%w+)"}') do
js[k] = v;
end
return js
end
function json_encode(map)
local msg = "{"
for k,v in pairs(map) do
msg = msg..'"'..k..'":"'..v..'",'
end
return string.sub(msg,0,-2)..'}'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment