Deserialize TClass from XData to Web Core

Dear all,
I'm trying to use the Web Core with XData (please note that I have no experience with Web Core).
I have a class in XData service with following structure :

Type
TCustomers = class;
TCustomerBranch = class;
TCustomerUsers = class;

TCustomers = Class
private
FID: Integer;
FName: string;
FBranchList: TObjectList< TCustomerBranch>;
public
constructor Create;
destructor Destroy; override;
published
property ID: Integer read FID write FID;
property Name: string read FName write FName;
property BranchList: TObjectList< TCustomerBranch > read FBranchList write FBranchList;
end;

TCustomerBranch = Class
private
FID: Integer;
FStreet: string;
FUsersList: TObjectList< TCustomerUsers >;
public
constructor Create;
destructor Destroy; override;
published
property ID: Integer read FID write FID;
property Street: string read FStreet write FStreet;
property FUsersList: TObjectList< TCustomerUsers > read FUsersList write FUsersList;
end;

TCustomerUsers = Class
private
FID: Integer;
FUserName: string;
public
published
property ID: Integer read FID write FID;
property UserName: string read FUserName write FUserName;
end;

The XData service returns the following structure :
{ "$id": 1,
"@xdata.type": "AppEntitiesWeb.TCustomers",
"ID": 0,
"Name": "CUSTOMER NAME",
"BranchList": [
{ "$id": 2,
"@xdata.type": "AppEntitiesWeb.TCustomerBranch ",
"ID": 0,
"Street": "Some Street",
"UsersList": [
{ "$id": 3,
"@xdata.type": "AppEntitiesWeb.TCustomerUsers",
"ID": 0,
"UserName": "Some User Name" } ] } ] }

Is there any way to deserialize the above Class TCustomers from XData to Web core as
VCL (e.g. : ACustomer := TJson.Deserialize< TCustomers >(JSONStr) ) ?
If not, is there a similar example I can use?

Thank you in advance!

You can parse the JSON and use it as a JSON object:

var JsonObject: TJSObject;
...
JsonObject := TJSJson.parseObject(StringJsonReturned);
// then read the properties as JSON:
Name := JS.ToString(JsonObject['Name']);

If you want to strong type it, you can use external classes:


TCustomers = class external name 'Object'
public
  ID: Integer;
  Name: string;
  BranchList: TArray<TCustomerBranch>;
end;

and the same for other classes, then you can simply cast TJSObject to TCustomers:

var Customers: TCustomers;
...
Customers := TCustomers(TJSJson.parseObject(StringJsonReturned));
// then read the properties as object
Name := Customers.Name;

Thank you very much Wagner!

One more question : when I use a strong type with external classes, how can I free the memory for each class?

Thank you again for your help!

1 Like

There is no need to free the memory in this case, it's JavaScript object. It will be collected automatically eventually.

Thank you very much Wagner!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.