#1 2023-03-30 18:34:22

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

assign a part of a TdocVariant to a record

I have a Tdocvariant that has the structure:

{
   "var1":[
      {
         "v1":[
            {
               "a":"1",
               "b":"2"
            }
         ],
         "v2":"something"
      }
   ],
   "var2":"aaa"
}

How can I assigned the var1 part of the above Tdocvariant to a pascal array of the following record type:

type 
  Dv1=record 
      a,b:rawutf8;
  end;

  Dvar1=record 
      v1:array of Dv1; 
      v2:rawutf8; 
  end;

var var1:array of Dvar1;

Last edited by dcoun (2023-03-30 18:39:51)

Offline

#2 2023-03-30 18:47:33

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: assign a part of a TdocVariant to a record

DynArrayLoadJson can do the job but it is a double conversion or am I missing something?

Offline

#3 2023-03-31 06:51:06

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

Re: assign a part of a TdocVariant to a record

I am not sure I understand what you do.

Offline

#4 2023-03-31 07:05:14

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: assign a part of a TdocVariant to a record

I have a Tdocvariant in a variant variable and I want to assign a part of it ("var1") to an other pascal variable (var1) which is a record with the same structure.
If I have understood ok, tdocvariant is a utf8string in reality, and not a real record inside it. Is it like that?

Offline

#5 2023-03-31 14:46:12

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: assign a part of a TdocVariant to a record

dcoun wrote:

I have a Tdocvariant in a variant variable and I want to assign a part of it ("var1") to an other pascal variable (var1) which is a record with the same structure.

Do you mean this?

type
  TArrItem = packed record
    a, b: RawUtf8;
  end;

  TDataItem = packed record
    v1: array of TArrItem;
    v2: RawUtf8;
  end;

  TDataItemList = array of TDataItem;

const
  JSON_DATA: RawJson = '{"var1":[{"v1":[{"a":"1","b":"2"},{"a":"3","b":"4"}],"v2":"something"}]}';
var
  doc: TDocVariantData;
  list: TDataItemList;
begin
  doc.InitJson(JSON_DATA, mFastFloat);
  if DynArrayLoadJson(list, Pointer(doc.A['var1'].ToJson), TypeInfo(TDataItemList)) <> Nil then
    ShowMessage(Utf8ToString(FormatUtf8('% - %', [list[0].v1[0].a, list[0].v2])));

Arnaud does it in a similar way in a few places.

With best regards
Thomas

Offline

#6 2023-03-31 18:53:41

dcoun
Member
From: Crete, Greece
Registered: 2020-02-18
Posts: 430

Re: assign a part of a TdocVariant to a record

That answers my question. Thanks a lot tbo.

Last edited by dcoun (2023-03-31 18:53:53)

Offline

Board footer

Powered by FluxBB