You are not logged in.
Considering this example:
J := _Safe(_Json('{a:{b:null,c:"null"}}'));
writeln(VariantToUtf8(J.GetValueByPath('a.b'))); //write "" (empty string)
writeln(VariantToUtf8(J.GetValueByPath('a.c'))); //write "null" (string with "null" content)
writeln(VariantToUtf8(J.GetValueByPath('a.d'))); //write "null" (string with "null" content)
If the property exists but is null it returns an empty string.
If the property does not exist (undefined in javascript) it returns "null" as a string.
Wouldn't it be more appropriate to return an empty string in both cases?
Last edited by macfly (2020-11-18 16:42:53)
Offline
Not, it is a feature, and documented as such.
It converts the variant into text.
So null is converted into 'null', but also 'null' is converted into 'null'.
Just as 1 is converted into '1' but also '1' is converted into '1'.
When converted into text, there is no way to know which type is the returned text.
So you have to check for null (VarIsEmptyOrNull) before using VariantToUTF8, if you want to make a distinction.
Offline
So null is converted into 'null', but also 'null' is converted into 'null'
In the context of the example above, null is converted to an empty string.
Please ignore the a.c example
The question is this:
If the property exists but is null it returns an empty string.
writeln(VariantToUtf8(J.GetValueByPath('a.b'))); //write "" (empty string)
If the property does not exist (undefined in javascript) it returns "null" as a string.
writeln(VariantToUtf8(J.GetValueByPath('a.d'))); //write "null" (string with "null" content)
Note that a.d does not exists.
Last edited by macfly (2020-11-18 17:17:44)
Offline
You have to test the GetValueByPath() result, or use another method to check if it exists.
I already do this verification with VarIsEmptyOrNull.
Or try to avoid with _Safe(J.Value['a']).U['d']
I am quoting to check if there is any inconsistency or not.
If it's correct, then it's ok.
Last edited by macfly (2020-11-18 17:20:02)
Offline