#1 2020-12-06 16:35:48

leus
Member
Registered: 2012-09-05
Posts: 79

Using TObjectList in service

Right now I have a service that receives a custom object.

  IMyService = interface(IInvokable)
    ['{E3186F91-F373-48A6-AE92-8931437D4AAF}']
    function MyMethod(const ClientId: RawUTF8; const MyClass: TMyClass): TCQRSResult;
  end;

TMyClass contains a TObjectList<TAnotherObject>. What is the proper, transparent way to handle this? Register a custom deserialization? I cannot use the "ClassName" hack because I don't control the requests being sent to this service, and I find the custom collections method really cumbersome.

Offline

#2 2020-12-06 16:47:51

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

Re: Using TObjectList in service

The easiest way is to use a T*ObjArray.

1. define

type 
  TAnotherObjArray = array of TAnother;

2. call in the initialization section of the unit:

initialization
  TJSONSerializer.RegisterObjArrayForJSON(TypeInfo(TAnotherObjArray), TAnother);

end.

Then you can use TAnotherObjArray as parameter, and you will have the proper serialization.
And no need to release the array: the framework will do it for you.

Also check the ObjArray*() methods to work with this TAnotherObjArray handler.

Offline

#3 2020-12-06 19:47:40

leus
Member
Registered: 2012-09-05
Posts: 79

Re: Using TObjectList in service

Okay - so no TObjectList but instead use Delphi arrays.  Got it.

Offline

#4 2020-12-07 09:37:06

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

Re: Using TObjectList in service

Note that on mORMot2, RegisterObjArrayForJSon() is needed only for Delphi 7-2009.
FPC and Delphi 2010 are able to recognize the "array of TSomeClass" RTTI and find the correlation between types.
One benefit of the big rewrite of RTTI cache in mORMot2. wink

Offline

Board footer

Powered by FluxBB