AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Send JSON over TCP connection

Ein Thema von mandoza · begonnen am 17. Sep 2019 · letzter Beitrag vom 18. Sep 2019
Antwort Antwort
Seite 1 von 2  1 2      
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
TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Send JSON over TCP connection

  Alt 17. Sep 2019, 15:32
Do you need a conversion from string to array of char ?
  Mit Zitat antworten Zitat
mandoza

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

AW: Send JSON over TCP connection

  Alt 17. Sep 2019, 15:56
Do you need a conversion from string to array of char ?
Yes, or any best suggestions to replace array of char Thank you
  Mit Zitat antworten Zitat
mjustin

Registriert seit: 14. Apr 2008
3.006 Beiträge
 
Delphi 2009 Professional
 
#4

AW: Send JSON over TCP connection

  Alt 17. Sep 2019, 18:25
Cross-post - see https://stackoverflow.com/questions/...tcp-connection
Michael Justin
  Mit Zitat antworten Zitat
mandoza

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

AW: Send JSON over TCP connection

  Alt 17. Sep 2019, 20:49
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 .
  Mit Zitat antworten Zitat
TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 09:12
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.
  Mit Zitat antworten Zitat
jobo

Registriert seit: 29. Nov 2010
3.072 Beiträge
 
Delphi 2010 Enterprise
 
#7

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:06
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.
Gruß, Jo
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#8

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:11
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?
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
mandoza

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

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:16
So many thanks TiGü , can you please provide the inverse way too ( to read the packet back * Deserialization * ) .

Again so many thanks
  Mit Zitat antworten Zitat
mandoza

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

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:17
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
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      

 

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 20:07 Uhr.
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