Einzelnen Beitrag anzeigen

mandoza

Registriert seit: 27. Apr 2018
16 Beiträge
 
#1

Send JSON over TCP connection

  Alt 17. Sep 2019, 10:14
Delphi-Version: 10.2 Tokyo
Hello, I've the following code that i want to have an optimized way to send it over a TCP connection , mainly the JSON converted class .
My question is :

WHAT'S THE BEST WAY TO ADD THIS SERIALIZED CLASS TO DeptInfo:Array of char; MAINLY THE OPTIMIZED WAY


Delphi-Quellcode:
type
  TDepartmentsInfo = Packed Record
    Packet_ID:Integer;
   BuffSize:Integer;
    DeptInfo:Array of char;// This will hold the JSON converted class
  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>);
  ...
  
  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);
   // Send this info to OUR Resquest Sender Socket
   DepartmentsInfo.Packet_ID:=1;
   DepartmentsInfo.BuffSize:=Sizeof(TDepartmentsInfo);
   
   {
    WHAT'S THE BEST WAY TO ADD THIS SERIALIZED CLASS TO DeptInfo:Array of char;
    MAINLY THE OPTIMIZED WAY
   }

   
   // NOW SEND
   SocketSendDeptInfo(@DepartmentsInfo, DepartmentsInfo.BuffSize);
   
  finally
    DepartmentList.Free();
  end;

end;
Thank you .
  Mit Zitat antworten Zitat