Please be aware, in this way you serialize the internal structure of the TDepartmentList.
Code:
'{"ownsObjects":true,"listHelper":[2],"items":[{"deptName":"Finance","deptID":10},{"deptName":"HUMAN RESOURCE","deptID":11}]}'
Solution:
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Collections,
REST.Json;
type
TDepartmentsInfo = Packed Record
Packet_ID: Integer;
DeptInfo: TArray<Char>; // This will hold the JSON converted class
function BuffSize: Integer;
end;
PDepartmentsInfo = ^TDepartmentsInfo;
type
TDepartment = class
strict private
FDeptName: string;
FDeptID: Integer;
public
property DeptName: string read FDeptName write FDeptName;
property DeptID: Integer read FDeptID write FDeptID;
end;
TDepartmentList = class(TObjectList<TDepartment>);
function TDepartmentsInfo.BuffSize: Integer;
begin
Result := SizeOf(Packet_ID) + (SizeOf(Char) * Length(DeptInfo));
end;
procedure SocketSendDeptInfo(const DepartmentsInfo: PDepartmentsInfo; BufferSize: Integer);
begin
end;
Procedure SerializeAndSend();
var
DepartmentList: TDepartmentList;
Department: TDepartment;
Json: string;
DepartmentsInfo: TDepartmentsInfo;
begin
DepartmentList := TDepartmentList.Create();
try
Department := TDepartment.Create();
Department.DeptName := 'Finance';
Department.DeptID := 10;
DepartmentList.Add(Department);
Department := TDepartment.Create();
Department.DeptName := 'HUMAN RESOURCE';
Department.DeptID := 11;
DepartmentList.Add(Department);
Json := TJson.ObjectToJsonString(DepartmentList);
DepartmentsInfo.Packet_ID := 1;
DepartmentsInfo.DeptInfo := Json.ToCharArray;
SocketSendDeptInfo(@DepartmentsInfo, DepartmentsInfo.BuffSize);
finally
DepartmentList.Free();
end;
end;
begin
SerializeAndSend;
end.