.
Simple JSON Encode/Decode in Pure Lua
More Lua code

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)

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.)


Comments so far....

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

— comment by Robert on July 25th, 2011 at 6:30am JST (6 months, 14 days ago) comment permalink

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

— comment by David Kolf on July 30th, 2011 at 7:30am JST (6 months, 8 days ago) comment permalink

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

— comment by Michael on October 1st, 2011 at 7:51am JST (4 months, 6 days ago) comment permalink

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

— comment by MaikB on October 21st, 2011 at 3:08am JST (3 months, 18 days ago) comment permalink

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 routines
While lines 24 and 111 have -- JSON = (loadfile "JSON:lua")() -- one-time load of the routines

That 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

— comment by Kyle (Singapore) on December 23rd, 2011 at 12:41pm JST (1 month, 15 days ago) comment permalink
Leave a comment...


All comments are invisible to others until Jeffrey approves them.

Please mention what part of the world you're writing from, if you don't mind. It's always interesting to see where people are visiting from.

More or less plain text — see below for allowed markup

You can use the following tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting