#1 mORMot 2 » How to deserialize Json to generic typed object » 2024-11-21 11:24:00

Tuna
Replies: 1

TDataResponse<TData>

TData is a generic type

TDataResponse= class
  private
    FId: Integer;
    FSuccess: Boolean;
    FMessage: string;
  public
    property Id: Integer read FId write FId;
    property Success: Boolean read FSuccess write FSuccess;
    property Message: string read FMessage write FMessage;
  end;

Response: TDataResponse<TData>.Create
JSONToObject(Response, ResponsePUtf8Char, JsonToObjectResult);

When I try to convert, the JsonToObjectResult became true but Response object is same with the created object. Am I missing something or its not possible to convert the object that has generic type property

function ConvertToObject<TResponse, TContent>(): TDataResponse<TResponse>;
  var
  ResponseStr: string;
  ResponseUTF8Str: UTF8String;
  ResponsePUtf8Char: PUtf8Char;
  Response: TDataResponse<TResponse>;
  JsonToObjectResult: boolean;

begin

ResponseStr :=ResponseStr := '{"data":{
"token":null,
"aboneUnvan":null,
"aboneInfo":null,
"availableService":{"isSms":true,"isTc":true,"isVergi":false},
"originators":[
{"id":null,"serviceId":null,"originatorName":"ABC"},
{"id":null,"serviceId":null,"originatorName":"ABCZ"},
{"id":null,"serviceId":null,"originatorName":"ABCDF"},
{"id":null,"serviceId":null,"originatorName":"ABCD"},
{"id":null,"serviceId":null,"originatorName":""}
],
"services":[
{"id":null,"name":"AVEAT","text":null},
{"id":null,"name":"TC","text":null},
{"id":null,"name":"TRKC","text":null},
{"id":null,"name":null,"text":null}
]},
"success":true,
"message":""}';

ResponseUTF8Str := UTF8String(ResponseStr);
ResponsePUtf8Char := PUtf8Char(ResponseUTF8Str);
Response := TDataResponse<TResponse>.Create;

JSONToObject(Response, ResponsePUtf8Char, JsonToObjectResult);
Result := Response;
end;
end;

The example TData type I try to convert:

GetUserInfoResponse = class
  private
    FToken: string;
    FAboneUnvan: string;
    FAboneInfo: string;
    FAvailableService: TAvailableService;
    FServices: TList<TService>;
    FOriginator: TList<TOriginator>;
  public
    property Token: string read FToken write FToken;
    property IsTFAboneUnvanc: string read FAboneUnvan write FAboneUnvan;
    property AboneInfo: string read FAboneInfo write FAboneInfo;
    property AvailableService: TAvailableService read FAvailableService write FAvailableService;
    property Originators: TList<TOriginator> read FOriginator write FOriginator;
    property Services: TList<TService> read FServices write FServices;
  end;
TAvailableService = class
  private
    FIsSms: boolean;
    FIsTc: boolean;
    FIsVergi: boolean;
  public
    property IsSms: boolean read FIsSms write FIsSms;
    property IsTc: boolean read FIsTc write FIsTc;
    property IsVergi: boolean read FIsVergi write FIsVergi;
  end;

  TOriginator = class
  private
    FId: integer;
    FServiceId: integer;
    FOriginatorName: integer;
  public
    property Id: integer read FId write FId;
    property ServiceId: integer read FServiceId write FServiceId;
    property OriginatorName: integer read FOriginatorName write FOriginatorName;
  end;

  TService = class
  private
    FId: integer;
    FName: string;
    FText: string;
  public
    property Id: integer read FId write FId;
    property Name: string read FName write FName;
    property Text: string read FText write FText;
  end;

Board footer

Powered by FluxBB