#1 2021-01-04 18:18:01

Milos
Member
Registered: 2021-01-04
Posts: 36

Struggling to get an array of objects from interface service

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

#2 2021-01-04 18:30:17

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

Re: Struggling to get an array of objects from interface service

You need to call TJSONSerializer.RegisterObjArrayForJSON before TInterfaceFactory.RegisterInterfaces.

Otherwise, TInterfaceFactory.RegisterInterfaces doesn't know this parameter is an T*ObjArray.

Offline

#3 2021-01-04 18:36:26

Milos
Member
Registered: 2021-01-04
Posts: 36

Re: Struggling to get an array of objects from interface service

AAH! It makes sense of course but my brain didn't see it...

Thanks! Merci beaucoup!

Offline

#4 2021-01-05 08:41:33

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

Re: Struggling to get an array of objects from interface service

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

#5 2021-01-05 16:31:33

Milos
Member
Registered: 2021-01-04
Posts: 36

Re: Struggling to get an array of objects from interface service

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

#6 2021-01-05 18:32:20

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

Re: Struggling to get an array of objects from interface service

mORMot 2 is still under tests and fixes... I hope it to be ready for production in a few weeks.

Offline

#7 2021-01-05 18:47:17

Milos
Member
Registered: 2021-01-04
Posts: 36

Re: Struggling to get an array of objects from interface service

Ah I see, good luck

Offline

Board footer

Powered by FluxBB