Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Send JSON over TCP connection (https://www.delphipraxis.net/201989-send-json-over-tcp-connection.html)

mandoza 17. Sep 2019 09:14

Delphi-Version: 10.2 Tokyo

Send JSON over TCP connection
 
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
Delphi-Quellcode:
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 .

TiGü 17. Sep 2019 14:32

AW: Send JSON over TCP connection
 
Do you need a conversion from
Delphi-Quellcode:
string
to
Delphi-Quellcode:
array of char
?

mandoza 17. Sep 2019 14:56

AW: Send JSON over TCP connection
 
Zitat:

Zitat von TiGü (Beitrag 1446648)
Do you need a conversion from
Delphi-Quellcode:
string
to
Delphi-Quellcode:
array of char
?

Yes, or any best suggestions to replace
Delphi-Quellcode:
array of char
Thank you

mjustin 17. Sep 2019 17:25

AW: Send JSON over TCP connection
 
Cross-post - see https://stackoverflow.com/questions/...tcp-connection

mandoza 17. Sep 2019 19:49

AW: Send JSON over TCP connection
 
Zitat:

Zitat von mjustin (Beitrag 1446681)

Thank you , But is there any issue in cross-posting my question ?
That was just to have multi and efficient replies ( cause i know there're many Delphi Gurus here and at Sackoverflow ....

Again thank you .

TiGü 18. Sep 2019 08:12

AW: Send JSON over TCP connection
 
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.

jobo 18. Sep 2019 09:06

AW: Send JSON over TCP connection
 
Zitat:

Zitat von mandoza (Beitrag 1446702)
Zitat:

Zitat von mjustin (Beitrag 1446681)

Thank you , But is there any issue in cross-posting my question ?

LOL, of course, there are several issues in doing so! Beside reading forum rules, is this Your first post and also the first time reading in a forum and You realy didn't get it?

The least You can do:
It is a matter of courtesy and respect to indicate the crosspost right from the start.

Neutral General 18. Sep 2019 09:11

AW: Send JSON over TCP connection
 
Zitat:

Zitat von jobo (Beitrag 1446821)
Zitat:

Zitat von mandoza (Beitrag 1446702)
Zitat:

Zitat von mjustin (Beitrag 1446681)

Thank you , But is there any issue in cross-posting my question ?

LOL, of course, there are several issues in doing so! Beside reading forum rules, is this Your first post and also the first time reading in a forum and You realy didn't get it?

The least You can do:
It is a matter of courtesy and respect to indicate the crosspost right from the start.

... It's just easier to see for each side what has already been posted (somewhere else).
It's not that big of a tragedy you make it out to be..
Courtesy and respect? Really? :roll:

mandoza 18. Sep 2019 09:16

AW: Send JSON over TCP connection
 
So many thanks TiGü , can you please provide the inverse way too ( to read the packet back * Deserialization * ) .

Again so many thanks

mandoza 18. Sep 2019 09:17

AW: Send JSON over TCP connection
 
Zitat:

Zitat von jobo (Beitrag 1446821)
Zitat:

Zitat von mandoza (Beitrag 1446702)
Zitat:

Zitat von mjustin (Beitrag 1446681)

Thank you , But is there any issue in cross-posting my question ?

LOL, of course, there are several issues in doing so! Beside reading forum rules, is this Your first post and also the first time reading in a forum and You realy didn't get it?

The least You can do:
It is a matter of courtesy and respect to indicate the crosspost right from the start.

OK sorry for that ... really


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:21 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz