#1 2019-06-12 14:51:08

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

Cutom records serialization - problem

my custom record is:

 TIndicatorExport = record
   Index : Integer;
   Value : Int64;
 // s : string;   <================= this field changes the behavior
 end;

custom writer is:

class procedure TIndicatorExportCustomWriter.CustomWriter(const aWriter: SynCommons.TTextWriter; const aValue);
var
  V: TIndicatorExport absolute aValue;
begin
  aWriter.AddJSONEscape(['I', V.Index, 'V', V.Value]);
end;

I register my custom writer by:

  SynCommons.TTextWriter.RegisterCustomJSONSerializer(TypeInfo(TIndicatorExport),
                                            nil,
                                            TIndicatorExportCustomWriter.CustomWriter);

And I try use of it:

  DynArray: TDynArray;
  Data: TArray<TIndicatorExport>;
  [..]
  DynArray.Init(TypeInfo(TArray<TIndicatorExport>), Data);
  Str := DynArray.SaveToJSON(False);

 
after executed last line the Str has '["00000000000000010000000000000000"]' string,
not my customized, because my custom writer is not called.

BUT... when I add additional field (string) to record  then custom serialization works properly!

Where is my mistake?

Last edited by jaclas (2019-06-12 14:51:30)

Offline

#2 2019-06-12 16:08:05

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

Re: Cutom records serialization - problem

It is a compiler limitation, not a framework bug: a record without any managed type doesn't have enough RTTI, IIRC.

One way of bypassing it is to register the dynamic array type, not the record type, for serialization.
Something like that:

type
  TIndicatorExports = array of TIndicatorExport;
...
SynCommons.TTextWriter.RegisterCustomJSONSerializer(TypeInfo(TIndicatorExports), nil, TIndicatorExportCustomWriter.CustomWriter);

Offline

Board footer

Powered by FluxBB