You are not logged in.
Pages: 1
Hi,
I have found some strange behaviour with TDynArray conversion to JSON (or maybe I am missing something).
If I define array of record which has only integer fields in definition, conversion to JSON fails, but if I add an string to record everything works as expected.
I use XE6 and tested it also with 10.2 CE (both 32bit) and results were the same, I used lastest nightlybuild of mORMot for test.
Definition for array is as such:
type
TPosition = packed record
FieldId:integer;
Width: integer;
// DummyString:string;
end;
type TPositions = array of TPosition;
In the following procedure content of TmpJSON is used as conversion target and I have added results in comments:
procedure TForm1.Button1Click(Sender: TObject);
var fPositions:TPositions;
DAPositions: TDynArray;
TmpPos:TPosition;
TmpJSON:RawUTF8;
begin
DAPositions.Init(TypeInfo(TPositions),fPositions);
DAPositions.Clear;
TmpPos.FieldId:=1;
TmpPos.Width:=50;
//test if record to JSON is ok
TmpJSON:=RecordSaveJSON(TmpPos, typeInfo(TPosition));
//OK we get TmpJSON='{"FieldId":1,"Width":50}'
DAPositions.Add(TmpPos);
TmpPos.FieldId:=2;
TmpPos.Width:=150;
DAPositions.Add(TmpPos);
//test if array to JSON is ok
TmpJSON:=DAPositions.SaveToJSON;
//ERROR: we get TmpJSON='[214748364801,644245094402]'
//
//if we add DummyString:string; to TPosition definition
//we get correct data: TmpJSON='[{"FieldId":1,"Width":50,"DummyString":""},{"FieldId":2,"Width":150,"DummyString":""}]'
end;
Kind regards,
Bostjan
Offline
It is a Delphi/FPC limitation: a record without any managed field has not sufficient RTTI.
You need to register the record as text first, using TTextWriter.RegisterCustomJSONSerializerFromText().
Offline
Thank you for your answer !
I solved my problem by changing record definiton, but since it was a simple case and I already wanted to try how custom serializer works, based on GutHub example (section 10.1.3.2.4. Text-based definition from SAD), I added this before SaveToJSON:
TTextWriter.RegisterCustomJSONSerializerFromText(TypeInfo(TPosition),'FieldId integer Width integer');
TmpJSON:=DAPositions.SaveToJSON();
but results were the same: [214748364801,644245094402].
I also tried:
'{FieldId integer Width integer}'; //error with registering
and
'A: record FieldId, Width: integer;'; //result: '[214748364801,644245094402]'
Is it required to register record (TPosition) or array serialization (TPositions) ?
Offline
Pages: 1