You are not logged in.
Hello, I searched a lot, found some answers but they didn't work for me, I must be doing something wrong and not seeing it.
I would like the interface to return an array of TSQLPerson objects. Instead, I seem to get an array of integers.
TSQLPerson = class(TSQLRecord)
private
fName: RawUTF8;
fBirthDate: TDateTime;
public
published
property Name: RawUTF8 index 50 read fName write fName;
property BirthDate: TDateTime read fBirthDate write fBirthDate;
end;
TSQLPersonObjArray = array of TSQLPerson;
ITest = interface(IInvokable)
['{3B4FEE39-C650-44C7-A6AF-0C9121342BBD}']
procedure OnePerson (out Person: TSQLPerson);
procedure Persons (out PersonObjArray: TSQLPersonObjArray);
end;
...
TInterfaceFactory.RegisterInterfaces([TypeInfo(ITest), TypeInfo(ITest)]);
TJSONSerializer.RegisterClassForJSON([TSQLPerson]);
TJSONSerializer.RegisterObjArrayForJSON(TypeInfo(TSQLPersonObjArray), TSQLPerson);
...
procedure TTest.Persons(out PersonObjArray: TSQLPersonObjArray);
var
Person : TSQLPerson;
begin
Person := TSQLPerson.Create;
Person.Name := 'John Doe';
Person.BirthDate := now - 365*30;
ObjArrayAdd(PersonObjArray, Person);
Person := TSQLPerson.Create;
Person.Name := 'Jane Doe';
Person.BirthDate := now - 365*40;
ObjArrayAdd(PersonObjArray, Person);
end;
OnePerson method works while the Persons method seems to return an array of integers.
Server debug output:
{"result":[[54783928,54783952]]}
I tried with TSynAutoCreateFields descendants instead of TSQLRecord but the outcome was the same.
What am I doing wrong?
Last edited by Milos (2021-01-04 18:18:23)
Offline
You need to call TJSONSerializer.RegisterObjArrayForJSON before TInterfaceFactory.RegisterInterfaces.
Otherwise, TInterfaceFactory.RegisterInterfaces doesn't know this parameter is an T*ObjArray.
Offline
AAH! It makes sense of course but my brain didn't see it...
Thanks! Merci beaucoup!
Offline
Note that on mORMot 2 + FPC or Delphi 2010+, you don't need to call RegisterObjArrayForJson(), since there is enough RTTI to know the class type hold by the array.
Offline
Thanks, I have just recently started to more seriously examine Mormot so I am not really familiar with version 2 and whether I should focus on that one instead, I think I will do some more testing and research with 1.18 but maybe re-think when it comes time to start with "heavy" development.
Offline
Ah I see, good luck
Offline