JSON conversions in dzVents fail since v3.0.9  [SOLVED]

Moderator: leecollings

Post Reply
mgugu
Posts: 208
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

JSON conversions in dzVents fail since v3.0.9

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: JSON conversions in dzVents fail since v3.0.9

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
mgugu
Posts: 208
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: JSON conversions in dzVents fail since v3.0.9  [SOLVED]

Post by mgugu »

Thank you waaren for answering.
WIth the isJSON function you proposed, json behaviour is now correct.
Thank you
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest