![]() |
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:
MAINLY THE OPTIMIZED WAY
DeptInfo:Array of char;
Delphi-Quellcode:
Thank you .
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; |
AW: Send JSON over TCP connection
Do you need a conversion from
Delphi-Quellcode:
to
string
Delphi-Quellcode:
?
array of char
|
AW: Send JSON over TCP connection
Zitat:
Delphi-Quellcode:
Thank you
array of char
|
AW: Send JSON over TCP connection
Cross-post - see
![]() |
AW: Send JSON over TCP connection
Zitat:
That was just to have multi and efficient replies ( cause i know there're many Delphi Gurus here and at Sackoverflow .... Again thank you . |
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. |
AW: Send JSON over TCP connection
Zitat:
The least You can do: It is a matter of courtesy and respect to indicate the crosspost right from the start. |
AW: Send JSON over TCP connection
Zitat:
It's not that big of a tragedy you make it out to be.. Courtesy and respect? Really? :roll: |
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 |
AW: Send JSON over TCP connection
Zitat:
|
AW: Send JSON over TCP connection
Zitat:
Please any other better way . |
AW: Send JSON over TCP connection
Hi mandoza, I would like to remind you, that we have a fine english speaking sibling to this forum found here:
![]() Sherlock |
AW: Send JSON over TCP connection
Zitat:
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. |
AW: Send JSON over TCP connection
Talking about overhead ... why not remove all overhead?
Code:
contains the same information as
[
{ "deptName": "Finance", "deptID": 10 }, { "deptName": "HUMAN RESOURCE", "deptID": 11 } ]
Code:
but with less chars (overhead).
{
"items": [ { "deptName": "Finance", "deptID": 10 }, { "deptName": "HUMAN RESOURCE", "deptID": 11 } ] } |
AW: Send JSON over TCP connection
Zitat:
Asking sames question in different communities without letting each other know, forces people to waste their power and time. That's disrespectful in my point of view. Although he apologized and seems to understand the problem, he didn't link to this post on SO. And pretending that's all new .. doesn't make it any better. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 17:33 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz