#1 2019-05-10 05:36:45

MikaK
Member
Registered: 2016-11-09
Posts: 14

Detecting types from docvariant

Hi,
I'm writing routine to compare two json objects (DocVariant). All elements in original must be in other json, but other json can contain some extra elements and order can vary.  For that I need to detect type of individual elements

a := _json('{"arr":[1,2,3],"integer":2,"isnull":null,"str":"somestring","float":3.14,"subobject":{"a":"b","c":1}}');
for I := 0 to a._count-1 do  
begin
  s := a.name(i);
  Writeln('Object name: ',s,' -------------');
  b := a._(i);
  if varisnull( b)  then
    Writeln('is null')
  else
  begin
    Writeln( 'Docvariantdata: ',Tdocvariantdata ( b).VarType );
    Writeln( 'kind: ',ord(Tdocvariantdata ( b).Kind) );
  end;
end;

Will print

Object name: arr -------------
Docvariantdata: 16396
kind: 0
Object name: integer -------------
Docvariantdata: 3
kind: 0
Object name: isnull -------------
is null
Object name: str -------------
Docvariantdata: 16396
kind: 0
Object name: float -------------
Docvariantdata: 6
kind: 0
Object name: subobject -------------
Docvariantdata: 16396
kind: 0

so array, string and object are exactly same! What i'm missing?
Of course I could convert b to string and check if it starts with [ or { but that somehow seems stupid smile

thanks
mika

Last edited by MikaK (2019-05-10 05:37:31)

Offline

#2 2019-05-10 06:33:43

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: Detecting types from docvariant

Instead of Tdocvariantdata ( b).Kind try _Safe(b).Kind. Or better yet, make a/b of PDocVariantData or TDocVariantData

Offline

#3 2019-05-10 07:01:05

MikaK
Member
Registered: 2016-11-09
Posts: 14

Re: Detecting types from docvariant

Thanks, with _safe(b) it worked.

Offline

Board footer

Powered by FluxBB