Is there any issue with that ( network latency ... etc ) ?
Please any other better way .
It's unnessacry overhead and can't properly parsed in other dev environments.
Imagine if someone with Javascript, C++ or something else want to parse your JSON with ownObjects and listHelper.
You can workaround this with a simple data transfer object (DTO).
Code:
'{"items":[{"deptName":"Finance","deptID":10},{"deptName":"HUMAN RESOURCE","deptID":11}]}'
Delphi-Quellcode:
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Department in 'Department.pas',
System.SysUtils,
REST.Json;
Procedure SerializeAndSend;
var
DepartmentList, DepartmentList2: TDepartmentList;
Department: TDepartment;
Json: string;
DepartmentsInfo: TDepartmentsInfo;
DepartmentStore: TDepartmentStore;
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);
DepartmentStore := TDepartmentStore.Create(DepartmentList.ToArray);
try
Json := TJson.ObjectToJsonString(DepartmentStore);
finally
DepartmentStore.Free;
end;
finally
DepartmentList.Free();
end;
DepartmentsInfo.Packet_ID := 1;
DepartmentsInfo.DeptInfo := Json.ToCharArray;
SocketSendDeptInfo(@DepartmentsInfo, DepartmentsInfo.BuffSize);
// Deserialization
DepartmentStore := TJson.JsonToObject<TDepartmentStore>(Json);
DepartmentList2 := TDepartmentList.Create();
try
DepartmentList2.AddRange(DepartmentStore.Items);
finally
DepartmentList2.Free;
end;
end;
begin
SerializeAndSend;
Readln;
end.
Delphi-Quellcode:
unit Department;
interface
uses
System.SysUtils,
System.Generics.Collections,
REST.Json,
System.Rtti;
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>);
TDepartmentStore =
class
private
FItems: TArray<TDepartment>;
public
constructor Create(
const AItems: TArray<TDepartment>);
property Items: TArray<TDepartment>
read FItems;
end;
procedure SocketSendDeptInfo(
const DepartmentsInfo: PDepartmentsInfo;
BufferSize: Integer);
implementation
function TDepartmentsInfo.BuffSize: Integer;
begin
Result := SizeOf(Packet_ID) + (SizeOf(Char) * Length(DeptInfo));
end;
procedure SocketSendDeptInfo(
const DepartmentsInfo: PDepartmentsInfo;
BufferSize: Integer);
begin
end;
constructor TDepartmentStore.Create(
const AItems: TArray<TDepartment>);
begin
FItems := AItems;
end;
end.