#1 2014-03-29 15:48:38

martin.suer
Member
Registered: 2013-12-15
Posts: 76

Record Serialization of records that contain multiple dynamic arrays

Hi again,

maybe there's an issue with record serialization when a record contains multiple different dynamic arrays like in the code example below.

type
  TSubAB = packed record
    a : RawUTF8;
    b : integer;
  end;
  TSubCD = packed record
    c : byte;
    d : RawUTF8;
  end;
  TAggregate = packed record
    abArr : array of TSubAB;
    cdArr : array of TSubCD;
  end;
const
  __TSubAB = 'a : RawUTF8; b : integer;';
  __TSubCD = 'c : byte; d : RawUTF8;';
  __TAggregate = 'abArr : array of TSubAB; cdArr : array of TSubCD;';
var
  ab0,ab1: TSubAB;
  cd0,cd1,cd2: TSubCD;
  agg,agg2: TAggregate;
  u,v,w : RawUTF8;
begin
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TSubAB),__TSubAB);
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TSubCD),__TSubCD);
  TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TAggregate),__TAggregate);

  ab0.a := 'AB0';
  ab0.b := 0;
  ab1.a := 'AB1';
  ab1.b := 1;
  cd0.c := 0;
  cd0.d := 'CD0';
  cd1.c := 1;
  cd1.d := 'CD1';
  cd2.c := 2;
  cd2.d := 'CD2';
  SetLength(agg.abArr,2);
  agg.abArr[0] := ab0;
  agg.abArr[1] := ab1;
  SetLength(agg.cdArr,3);
  agg.cdArr[0] := cd0;
  agg.cdArr[1] := cd1;
  agg.cdArr[2] := cd2;

  u := RecordSaveJSON(agg,TypeInfo(TAggregate));
  w := u;
  RecordLoadJSON(agg2,@u[1],TypeInfo(TAggregate));
  v := RecordSaveJSON(agg2,TypeInfo(TAggregate));

  Writeln ('w = ',UTF8ToString(w));
  Writeln ('v = ',UTF8ToString(v));

That code creates a record, serializes it to JSON, then deserializes it to a new record. Both records should then contain the same values but don't.

The second dynamic array in the TAggregate record gets lost...

Output of the above code:

w = {"abArr":[{"a":"AB0","b":0},{"a":"AB1","b":1}],"cdArr":[{"c":0,"d":"CD0"},{"c":1,"d":"CD1"},{"c":2,"d":"CD2"}]}
v = {"abArr":[{"a":"AB0","b":0},{"a":"AB1","b":1}],"cdArr":[]}

Is this an issue or am I doing sth. wrong ?

Martin

Offline

#2 2014-03-29 16:44:57

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

Re: Record Serialization of records that contain multiple dynamic arrays

Should be fixed by http://synopse.info/fossil/info/e425235fbb

Thanks for the feedback.
With such code to reproduce the issue, fixing it is a real pleasure!

Offline

Board footer

Powered by FluxBB