Page 1 of 1
JSON conversions in dzVents fail since v3.0.9
Posted: Wednesday 17 June 2020 17:23
by mgugu
Since version 3.0.9 I get errors in JSON conversion (fromJSON).
I found this is due to the new function isJSON which is now used in the fromJSON function to check json validity.
The isJSON documentation says that json string must be enclosed between {} why ?
Example:
Code: Select all
print(domoticz.utils.isJSON('{["a","b"]}') and 'true' or 'false') => true, while {["a","b"]} is not a correct json
print(domoticz.utils.isJSON('["a","b"]') and 'true' or 'false') => false, while ["a","b"] is a correct json
So scripts written with version 3.0.2 will not work anymore
Re: JSON conversions in dzVents fail since v3.0.9
Posted: Thursday 18 June 2020 10:47
by waaren
mgugu wrote: ↑Wednesday 17 June 2020 17:23
Since version 3.0.9 I get errors in JSON conversion (fromJSON).
I found this is due to the new function isJSON which is now used in the fromJSON function to check json validity.
The isJSON documentation says that json string must be enclosed between {} why ?
Example:
Code: Select all
print(domoticz.utils.isJSON('{["a","b"]}') and 'true' or 'false') => true, while {["a","b"]} is not a correct json
print(domoticz.utils.isJSON('["a","b"]') and 'true' or 'false') => false, while ["a","b"] is a correct json
So scripts written with version 3.0.2 will not work anymore
Thx for reporting.
The isJSON function is inserted to catch invalid JSON strings before they are passed to the jsonParser:decode function. ( It cannot catch all exceptions so for some JSON's the string will be passed to the decode function and will stil cause an error there.)
For your example the isJSON function is set stricter then the jsonParser itself and that is not what was intended.
Can you try again after replacing the self.isJSON function in <domoticz dir>/dzVents/runtime/Utils.lua with
Code: Select all
function self.isJSON(str, content)
local str = str or ''
local content = content or ''
local jsonPatternOK = '^%s*%[*%s*{.+}%s*%]*%s*$'
local jsonPatternOK2 = '^%s*%[.+%]*%s*$'
local ret = ( str:match(jsonPatternOK) == str ) or ( str:match(jsonPatternOK2) == str ) or content:find('application/json')
return ret ~= nil
end
Re: JSON conversions in dzVents fail since v3.0.9 [SOLVED]
Posted: Thursday 18 June 2020 13:30
by mgugu
Thank you waaren for answering.
WIth the isJSON function you proposed, json behaviour is now correct.
Thank you