#1 2014-08-26 12:34:54

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Serialization of object which implement generics

Dear all,

I want to create service of TServiceCoupon and implement TServiceCoupon.Get to return Json result of TResponseJson<TSQLCouponMst>
But during runtime, I got the exception "unexcepted type TResponseJson<DataMode.TSQLCouponMst> - use TJONSerializer.RegisterCustomSerializer()
I think I got something wrong in coding/definition and no need to write custom serializer.
Would anyone have any comment for my thinking?
Thank a lot!

Regards,
  HK City

  TResponseJson<T: class> = class (TObjectList<T>)
  private
    Fdata: TObjectList<T>;
    Fsuccess: boolean;
    Fmsg: String;
    procedure Setdata(const Value: TObjectList<T>);

  published
    constructor Create;
    property success : boolean read Fsuccess write Fsuccess;
    property msg : String read Fmsg write Fmsg;
    property data: TObjectList<T> read Fdata write Setdata;
  end;

  TSQLCouponMst = class(TSQLRecord)
  protected
    fidCouponMst: Int64;
    fshopName: RawUTF8;
    fcouponDesc: RawUTF8;
    feffDateDesc: RawUTF8;
    fcouponFileName: RawUTF8;
    fcrtDate: TDateTime;
    fcrtBy: RawUTF8;
  published
    property idCouponMst: Int64 read fidCouponMst write fidCouponMst;
    property shopName: RawUTF8 index 50 read fshopName write fshopName;
    property couponDesc: RawUTF8 index 200 read fcouponDesc write fcouponDesc;
    property effDateDesc: RawUTF8 index 50 read feffDateDesc write feffDateDesc;
    property couponFileName: RawUTF8 index 50 read fcouponFileName write fcouponFileName;
    property crtDate: TDateTime read fcrtDate write fcrtDate;
    property crtBy: RawUTF8 index 50 read fcrtBy write fcrtBy;
  end;

  TServiceCoupon = class(TInterfacedObject, ICoupon)
  public
    procedure Get(const id: integer; out response : TResponseJson<TSQLCouponMst>);
  end;

Offline

#2 2014-08-26 15:43:55

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

Re: Serialization of object which implement generics

Using such a generic TResponseJson<T: class> type is not supported yet.
And it sounds a bit complicated to me to create such a "response" type over our service interfaces.
It is the purpose of our service implementation to process responses, and offer directly-end user types.

IMHO the easiest is simply to use a RawJSON kind of parameter, then use good old TSQLTableJson and its TSQLTable.ToObjectList<T>: TObjectList<T> method, if you want to consume the result with generics.

Online

#3 2014-08-27 01:00:08

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Serialization of object which implement generics

I will try to your suggested approach. Thank for your advice

Offline

#4 2014-08-27 03:08:07

hkcity1111
Member
Registered: 2014-08-18
Posts: 14

Re: Serialization of object which implement generics

Thank ab. Finally I use this method to output for my json structure

    procedure Get(const id: integer; out success: boolean; out msg: string; out data: RawJSON);

Offline

#5 2014-08-27 07:28:24

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

Re: Serialization of object which implement generics

Or just use a record!

Records are great for DTO.

Online

Board footer

Powered by FluxBB