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
TiGü

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

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 08: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
mandoza

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

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 09:25
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}]}'
Is there any issue with that ( network latency ... etc ) ?
Please any other better way .
  Mit Zitat antworten Zitat
Benutzerbild von Sherlock
Sherlock

Registriert seit: 10. Jan 2006
Ort: Offenbach
3.811 Beiträge
 
Delphi 12 Athens
 
#3

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 09:34
Hi mandoza, I would like to remind you, that we have a fine english speaking sibling to this forum found here: en.delphipraxis.net.

Sherlock
Oliver
Geändert von Sherlock (Morgen um 16:78 Uhr) Grund: Weil ich es kann
  Mit Zitat antworten Zitat
TiGü

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

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:07
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.
  Mit Zitat antworten Zitat
Schokohase
(Gast)

n/a Beiträge
 
#5

AW: Send JSON over TCP connection

  Alt 18. Sep 2019, 10:14
Talking about overhead ... why not remove all overhead?
Code:
[
  {
    "deptName": "Finance",
    "deptID": 10
  },
  {
    "deptName": "HUMAN RESOURCE",
    "deptID": 11
  }
]
contains the same information as
Code:
{
  "items": [
    {
      "deptName": "Finance",
      "deptID": 10
    },
    {
      "deptName": "HUMAN RESOURCE",
      "deptID": 11
    }
  ]
}
but with less chars (overhead).
  Mit Zitat antworten Zitat
Antwort Antwort

 

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 00: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