#1 2019-02-24 08:59:45

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

TDocVariantData to variant or how to add to an array of objects

In a JSON of variable form and depth there are some properties who are arrays of objects, like "fek" in the following example:

{
 ...
  "extraFieldValues": {
    "fek": [
      {
        "aa": "",
        "issue": "",
        "issueyear": ""
      }
    ],
...
}

I have a PDocVariantData pointer pointing to "fek" and I want to add one more object in the array. However I can't use the AddItem method, because it requires a variant as parameter, so the following line produces a compilation error with message "Incompatible types: 'Variant' and 'TDocVariantData'".

pdoc^.AddItem(pdoc^._[0]^);

Is there any way to typecast a TDocVariantData as a Variant and if not, how can I add to the "fek" array of the above example one more of its contained objects?

Offline

#2 2019-02-24 10:48:40

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

Re: TDocVariantData to variant or how to add to an array of objects

Variant(aDocVariant)

Offline

#3 2019-02-24 14:27:54

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Re: TDocVariantData to variant or how to add to an array of objects

Thanks a lot! I had tried this typecast:

pdoc^.AddItem(Variant(pdoc^._[0]^));

but it raised a run time error with message "Invalid variant type".
After your message I used a local TDocVariantData variable, named docVData and it works! smile

docVData := pdoc^._[0]^;
pdoc^.AddItem(Variant(docVData));

Last edited by damiand (2019-02-24 14:28:34)

Offline

#4 2019-02-24 16:20:46

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

Re: TDocVariantData to variant or how to add to an array of objects

But I guess that for your demand, the cleanest is perhaps to write:

pdoc^.AddItem(pdoc^.Values[0]);

Offline

#5 2019-02-24 19:22:54

damiand
Member
From: Greece
Registered: 2018-09-21
Posts: 94

Re: TDocVariantData to variant or how to add to an array of objects

ab wrote:

But I guess that for your demand, the cleanest is perhaps to write:

pdoc^.AddItem(pdoc^.Values[0]);

That was my initial thought and attempt, but it raises an EVariantBadVarTypeError exception with message 'Invalid variant type'.

Offline

Board footer

Powered by FluxBB