#1 2019-08-08 16:30:28

jaclas
Member
Registered: 2014-09-12
Posts: 215

Serialize array of records using TDynArray and error on deserialize

types:

type
 TFieldData = record
    FieldName : UTF8String;
    FieldType : TFieldType;
    FieldValue : Variant;
  end;

  TFieldDataArray = TArray<TFieldData>;

code:

var
  s: RawUTF8;
  Tab : TFieldDataArray;
  DynArray : TDynArray;
  OtherDynArray: TDynArray;
begin
  //prepare array
  SetLength(Tab, 2);

  //prepare records
  Tab[0].FieldName := 'name';
  Tab[0].FieldType := ftString;
  Tab[0].FieldValue := 'igor';
  Tab[1].FieldName := 'count';
  Tab[1].FieldType := ftInteger;
  Tab[1].FieldValue := 15;

  // prepare DynArray
  DynArray.Init(TypeInfo(TArray<TFieldData>), Tab);

  // serialize array of records to JSON
  s := DynArray.SaveToJSON(False);  // s = [{"FieldName":"name","FieldType":1,"FieldValue":"igor"},{"FieldName":"count","FieldType":3,"FieldValue":15}]
  
  //trying to deserialize and AV
  OtherDynArray.LoadFromJSON(pointer(s));  <======== exception

I get exception:

Access violation at address 00697DF3 in module 'demo.exe''. Write of address 75174F70

How do properly deserialize JSON to array od records?

Offline

#2 2019-08-08 16:39:31

Chris75018
Member
From: France
Registered: 2012-09-14
Posts: 26
Website

Re: Serialize array of records using TDynArray and error on deserialize

Hi,

did you try to initialize OtherDynarray ?
Like OtherDynarray.Init(TypeInfo(TArray<TFieldData>), Tab)

Offline

#3 2019-08-08 16:45:03

jaclas
Member
Registered: 2014-09-12
Posts: 215

Re: Serialize array of records using TDynArray and error on deserialize

Ehhh, I forgot that TDynArray is just a wrapper! Thx!

Offline

Board footer

Powered by FluxBB