Simple JSON Encode/Decode in Pure Lua
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 Lightroom, but they're pure Lua 5, so can be used anywhere Lua is.
Download JSON.lua
Version 20111207.5 (version 5, Dec 7, 2011)
Version 20111207.5 (version 5, Dec 7, 2011)
Full docs are in the code itself, but basic use is:
JSON = (loadfile "JSON.lua")() -- one-time load of the routines local lua_value = JSON:decode(raw_json_text) -- decode example local raw_json_text = JSON:encode(lua_table_or_value) -- encode example local pretty_json_text = JSON:encode_pretty(lua_table_or_value) -- "pretty printed" version
Enjoy.
(You might also be interested in this comparison of Lua JSON packages.)
Leave a comment...
When I add this line to a plugin I am writing I get an error from Lightroom
MyPluginApi:28 cannot open JSON:lua: No such file or directory
The thing that makes no sense to me is that the JSON.lua file is located in the same directory as MyPluginApi.lua
Do you have an example plugin that might show the usage of this JSON library working?
Thanks
It looks as if you have a colon before ‘lua’ rather than a dot. —Jeffrey
Hi, I’ve recently noticed your module and included it in my comparison of JSON modules on the Lua wiki. Your module is one of the better pure-Lua JSON implementations and I guess I wouldn’t have needed to write my own if I had found yours earlier. I hope you don’t mind the inclusion in the list.
Thanks for the link, and for the tests. I’ve pushed a new version that addresses some of the problems you found in your tests. Thanks! —Jeffrey
Hi,
thanks for this. I’ve just checked out the file, but I didn’t find any info regarding the license type, just your copyright notice.
From a legal perspective, if the code comes without a license everyone can have a look but no one can use it, for any type of project. So are there any strings attached?
Cheers,
Michael
Feel free to use it as you like. —Jeffrey
I really like to use your module, but I can’t unless there is a license that clearly grants me permission to use it. For example MIT, like lua license itself, or BSD 2-clause.
Best,
Maik
I made it and I said you can use it. That should be enough. —Jeffrey
Err… something seems to be up with the comments in the file:
Lines 14 and 123 have
-- JSON = (loadfile "JSON.lua")() -- one-time load of the routinesWhile lines 24 and 111 have
-- JSON = (loadfile "JSON:lua")() -- one-time load of the routinesThat JSON.lua / JSON:lua is a bit inconsistent… Is that intentional? (Considering that Lightroom just gave me the error “attempt to call a nil value” when I tried loading JSON:lua, I doubt it…)
Also, THANK YOU for publishing this. It’s making my first foray into Lightroom plugin development (and Lua) so much easier!
(While I’m at it, any pointers on using relative paths in loadfile(), or, alternatively, finding out with directory is Lightroom’s working directory so I can build a relative path from that? Doing loadfile(JSON.lua) falls over with
File not found, but putting the absolute path in works fine, but… won’t work for anyone but me, which is not an optimal solution.And Lightroom’s file access is too fast to show up in Process Explorer, so I can’t get anything from there.)
Oops, thanks, I thought I fixed that once, but apparently not in my master source, so it reared its ugly head again. It should be dot-”lua” in all situations, so I’ve fixed it (again), thanks. As for the path stuff, try loadfile(LrPathUtils.child(_PLUGIN.path, “JSON.lua”)) or the like. —Jeffrey