Glad to hear that the latest is working well for you.
Good idea! Because the openLuup.json module is not dependent on any other openLuup modules, it doesn’t report its presence in the logs, so the version info doesn’t appear in the startup log.
It’s easy to do an explicit check:
if json.C then print "using Cjson" end
so I’ll put that in the log and perhaps on a console page.
OK - looks like cjson did install OK. AltUI didn’t find it, because it’s command is looking for json Lua files but cjson is not Lua. I tried your test but that failed but this indicated it was available:
local ok, result = pcall(require, 'cjson')
local json = package.loaded['cjson']
if (json) then
print('Found cjson')
else
print('cjson not found')
end
return true
I installed cjson on a PI and works just fine running the latest openLuup development branch.
I am also using it in one of my plugins in a way that works on openLuup (with cjson) and Vera (without). It is based on what AK did in openLuup, with some changes as Vera would not take that for some reason. The cjson.decode only works on a perfect json string, else it trows an exception. You can use cjson.safe.decode then it will not throw the exceptions.
local cjson = require("cjson")
local dkjson = require("dkjson")
local json
-- Wrapper for more solid handling for cjson as it trows a bit more errors that I'd like.
local function jsonAPI()
local is_cj, is_dk
local function _init()
is_cj = type(cjson) == "table"
is_dk = type(dkson) == "table"
end
local function _decode(data)
if is_cj then
local ok, res = pcall(cjson.decode, data)
if ok then return res end
end
local res, pos, msg = dkjson.decode(data)
return res, msg
end
local function _encode(data)
-- No special chekcing required as we must pass valid data our selfs
if is_cj then
return cjson.encode(data)
else
return dkjson.encode(data)
end
end
return {
Initialize = _init,
decode = _decode,
encode = _encode
}
end
json = jsonAPI()
json.Initialize()
local res = json.decode("{some json}")