{"id":1584,"date":"2010-08-01T01:56:58","date_gmt":"2010-07-31T16:56:58","guid":{"rendered":"https:\/\/regex.info\/blog\/lua\/json"},"modified":"2010-08-01T01:56:58","modified_gmt":"2010-07-31T16:56:58","slug":"json","status":"publish","type":"page","link":"https:\/\/regex.info\/blog\/lua\/json","title":{"rendered":"Simple JSON Encode\/Decode in Pure Lua"},"content":{"rendered":"\n\n<div style='float:right; border:solid 1px #88F; margin: 0 0 20px 2em; padding: 15px 20px; background-color: #922'>\n<a href='\/blog\/lua'>More Lua code<\/a>\n<\/div>\n\n\n\n<p>I've coded up some simple <a href='http:\/\/json.org\/'\nclass='quiet'>JSON<\/a> encode\/decode routines in pure <a class='quiet'\nhref='http:\/\/www.lua.org\/'>Lua<\/a> and thought I'd share them in case\nanyone else would find them useful. <span class='nobr'>I use them<\/span> in Adobe Lightroom, but they're\npure <span class='nobr'>Lua 5,<\/span> so can be used anywhere Lua is.<\/p>\n\n<div style='font-size:75%; width:200px;float:right; border:solid 1px #555; padding:5px;text-align:center'>\n<a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by\/3.0\/deed.en_US\">\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/i.creativecommons.org\/l\/by\/3.0\/88x31.png\" width=\"88\" height=\"31\"\nalt=\"Creative Commons License\"\nid=\"i88x31\"\nstyle=\"border-width:0\"\ntitle=\"Creative Commons License\"\/><\/a>\n<br\/>\n<span xmlns:dct=\"http:\/\/purl.org\/dc\/terms\/\" property=\"dct:title\">JSON Encode\/Decode in Pure LUA<\/span> by\n<a xmlns:cc=\"http:\/\/creativecommons.org\/ns#\" href=\"\/blog\/lua\/json\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Jeffrey Friedl<\/a>\nis licensed under a <a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by\/3.0\/deed.en_US\">Creative Commons <span class='nobr'>Attribution 3.0<\/span> Unported License<\/a>.\n<\/div>\n\n<div style='margin: 0 auto; width:50%; text-align:center; padding: 10px 0' class='bg-A'><a href='\/code\/JSON.lua' class='quiet'>Download <b>JSON.lua<\/b><\/a>\n<br\/><span class='nobr'>Version 20211016.28 (version 28: October 16, 2021)<\/span>\n<\/div>\n\n\n<style type=\"text\/css\">\nspan.comment  { color : #BB0 }\nspan.variable { color: #800 }\nspan.object   { color: cyan }\nspan.method   { color: green }\nspan.pseudo   { color: gray }\n<\/style>\n<p>Full docs and changelog are in the code itself, but basic use is:<\/p>\n<div style='border-left: 3px solid #444; margin-left:1em; padding: 5px 1em; background-color:#222'>\n<pre>\n<span class='object'>JSON<\/span> = (loadfile \"JSON.lua\")() <span class='comment'>-- <i>one-time load of the routines<\/i><\/span>\n\n\nlocal <span class='variable'>lua_value<\/span> = <span class='object'>JSON<\/span><span class='method'>:decode(<\/span><span class='pseudo'>raw_json_text<\/span><span class='method'>)<\/span> <span class='comment'>-- <i>decode example<\/i><\/span>\n\n\nlocal <span class='variable'>raw_json_text<\/span>    = <span class='object'>JSON<\/span><span class='method'>:encode(<\/span><span class='pseudo'>lua_table_or_value<\/span><span class='method'>)<\/span>        <span class='comment'>-- <i>encode example<\/i><\/span>\nlocal <span class='variable'>pretty_json_text<\/span> = <span class='object'>JSON<\/span><span class='method'>:encode_pretty(<\/span><span class='pseudo'>lua_table_or_value<\/span><span class='method'>)<\/span> <span class='comment'>-- <i>\"pretty printed\" version<\/i><\/span>\n<\/pre>\n<\/div>\n\n<p>Enjoy.<\/p>\n\n<p>(You might also be interested in <a href='http:\/\/lua-users.org\/wiki\/JsonModules'>this comparison of Lua JSON packages<\/a>.)<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>I've coded up some simple JSON encode\/decode routines in pure Lua and thought I'd share them in case anyone else would find them useful. I use them in Adobe Lightroom, but they're pure Lua 5, so can be used anywhere Lua is.<\/p> <p>Full docs and changelog are in the code itself, but basic use is:<\/p> <p>Enjoy.<\/p> <p>(You might also be interested in this comparison of Lua JSON packages.)<\/p>","protected":false},"author":1,"featured_media":0,"parent":1222,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"footnotes":""},"_links":{"self":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1584"}],"collection":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/comments?post=1584"}],"version-history":[{"count":0,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1584\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/pages\/1222"}],"wp:attachment":[{"href":"https:\/\/regex.info\/blog\/wp-json\/wp\/v2\/media?parent=1584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}