#1 2020-11-18 16:38:38

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

VariantToUtf8 and null/undefined properties in DocVariant

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

#2 2020-11-18 17:01:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: VariantToUtf8 and null/undefined properties in DocVariant

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

#3 2020-11-18 17:11:42

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: VariantToUtf8 and null/undefined properties in DocVariant

ab wrote:

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

#4 2020-11-18 17:15:53

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,661
Website

Re: VariantToUtf8 and null/undefined properties in DocVariant

You have to test the GetValueByPath() result, or use another method to check if it exists.

Offline

#5 2020-11-18 17:18:37

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: VariantToUtf8 and null/undefined properties in DocVariant

ab wrote:

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

Board footer

Powered by FluxBB