There is already a large number of options for JSON libraries available to Lua users (see [url=http://lua-users.org/wiki/JsonModules]http://lua-users.org/wiki/JsonModules[/url])
None is perfect, only some are pure Lua, some are quite slow. I wrote my own - probably the last thing you should do in this situation - partly as an exercise in learning Lua, but also to embed into my own applications. It’s no doubt not perfect either, but it’s small (and beautifully formed) and there’s been some interest (I have posted an earlier version elsewhere.)
So here’s the latest.
A few features:
[ul][li]it has two basic function calls: [tt]encode[/tt] and [tt]decode[/tt][/li]
[li]they both return two parameters: (1) required conversion or nil, (2) error message if first return is nil[/li]
[li]include it in your code with [tt]json = require “akb-json”[/tt] and call with [tt]JSON=json.encode(LUA); LUA=json.decode(JSON);[/tt][/li]
[li]numbers bigger than json.default.huge (which may be changed, default is 8.88e888) are treated as infinity[/li]
[li]json.default.max_array_length = 1000, and may be changed (there to guard against things like [tt]json.encode{[1e6]=1}[/tt])[/li]
[li]supports unicode escapes and UTF-8 encoding of Basic Multilingual Plane codepoints[/li]
[li]returns an error if given a self-referencing structure to encode[/li]
[li]returns an error if encoding a table with mixed numeric and string indices (JSON can’t represent this)[/li]
[li][tt]json.version[/tt] returns the library version number[/li][/ul]
I have done comprehensive unit testing including about 100 different cases, half of which are valid conversions, and three more extensive files (timings on a 2.66 GHz Intel Core 2 Duo iMac, about 20x the speed of a VeraLite):
Hey Akbooer,
Just happened to be searching some JSON module and stumbled upon yours here. I tried to upload akb-json to VeraEdge and tried to run the following code in “Test Luup Code” page and got some errors:
04/08/15 23:17:21.718 JobHandler_LuaUPnP::HandleActionRequest argument Code=do
local akbjson = require "akb-json"
local http = require "socket.http"
local loginUrl = "SomeURLReturnJSON"
local status, body, headers
body, status, headers = http.request(loginUrl)
luup.log("BODY: " .. body)
local ret, msg = akbjson.decode(body)
luup.log(ret["data"]["sid"])
end <0x748d0520>
02 04/08/15 23:17:21.719 e[33;1mluup_require duplicate akb-jsone[0m <0x748d0520>
50 04/08/15 23:17:22.254 luup_log:0: BODY: {"data":{"sid":"hDcOkI4ihOe6c1380LSN011457"},"success":true}
<0x748d0520>
01 04/08/15 23:17:22.255 e[31;1mLuaInterface::StartEngine failed run: 0 [string "do..."]:8: attempt to call field 'decode' (a nil value)e[0m <0x748d0520>
01 04/08/15 23:17:22.255 e[31;1mJobHandler_LuaUPnP::RunLua failed: do
local akbjson = require "akb-json"
local http = require "socket.http"
local loginUrl = "SomeURLReturnJSON"
local status, body, headers
body, status, headers = http.request(loginUrl)
luup.log("BODY: " .. body)
local ret, msg = akbjson.decode(body)
luup.log(ret["data"]["sid"])
ende[0m <0x748d0520>
It seemed that I did not correctly load the module? Do you know what I am doing wrong?
I uploaded akb-json.lua from Developer Apps → Luup Files.
I have DataYours, Nest plugin, PLEG, Foscam HD, Virtual Switch apps installed.
I have tried adding the module statement: module(‘akb-json’,package.seeall). With this statement, the error message “luup_require duplicate akb-jsone” went away. But “LuaInterface::StartEngine failed run: 0 [string “do…”]:8: attempt to call field ‘decode’ (a nil value) <0x748d0520>” is still there.
I have tried this akb-json.lua outside of Vera (on my laptop) and it works fine for some simple JSON strings I quickly picked.
Have you got anything in your startup code which might be causing this? It’s really difficult to diagnose without full contextual information.
I can only suppose there is another copy of that file, in another location, which something else has already loaded.
DataYours has one, but it’s called something else. You could use that, but it would be good to get to the bottom of this. There’s nothing fundamentally wrong with the module, as you have tested out yourself.
Ah ha! I didn’t put the file under /usr/lib/lua. After putting the file there and reload the engine, my test worked. Didn’t know I have to put it there. So I guess only plugins load LUA files from /etc/cmh-ludl?
Thanks, Abkbooer!
Best Home Automation shopping experience. Shop at Ezlo!