#1 2024-11-21 11:24:00

Tuna
Member
Registered: 2024-11-21
Posts: 1

How to deserialize Json to generic typed object

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;

Last edited by Tuna (2024-11-21 11:53:09)

Offline

#2 2024-11-21 12:53:11

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

Re: How to deserialize Json to generic typed object

We use regular/FPC-compatible for classes.
So you need to define the serialized properties as "published", not public.

Please follow the forum rules, and put your code not in the thread, but e.g. in a gist, for direct compilation.

Offline

Board footer

Powered by FluxBB