#1 2020-06-19 15:23:23

konsul41
Member
Registered: 2020-05-21
Posts: 24

Unserialise array

Serwer site

TFirmaObjArray = array of TFirma;

TFirma = class(TPersistentWithCustomCreate)
...
    property nazwa : RawUTF8 index 250 read fFirmanazwa write fFirmanazwa;
...
  end;

TKonfiguracja = class(TPersistentWithCustomCreate)
  private
    ffirma: TFirma;
    ffir: TFirmaObjArray;
    function GetFirmaCount: Integer;
  public
    constructor Create; Override;
    Destructor Destroy; Override;
    function Add(): TFirma;
    property FirmaCount : Integer read GetFirmaCount;

  published
    property firma   : TFirma read ffirma write ffirma;
    property fir: TFirmaObjArray read ffir write ffir;
  end;


serialise
IRemoteSQL = interface(IInvokable)
    ['{8ECE1EBB-49CB-436E-8D36-6284B55189C1}']
    function  KonfiguracjaGet():RawUTF8;
    ...
  end;

function TServiceRemoteSQL.KonfiguracjaGet: RawUTF8;
var Konfiguracja : TKonfiguracja;
    Firma : TFirma;
begin

  Konfiguracja := TKonfiguracja.Create;
  try
    Konfiguracja.Firma:= TFirma.Create;
    Konfiguracja.Firma.nip:='test';

    with Konfiguracja.Add() do
    begin
      nazwa :='Firma 1';
      miejscowosc:='Łomża';
    end;
    ...
    TJSONSerializer.RegisterObjArrayForJSON([TypeInfo(TFirmaObjArray), TFirma]);

    result := ObjectToJSON(Konfiguracja, []);
 

result is
{
    "firma": {
        "IDFirmy": 0,
        ...
    },
    "fir": [{
            "nazwa": "Firma 1",...
        },
...
                {
            "nazwa": "Firma 4",...
        }
    ]
}
Client Site

JSON:=mainform.fService.KonfiguracjaGet();
  Konfiguracja := TKonfiguracja.Create;
  JSONToObject(Konfiguracja,@JSON[1],Valid);


How to reverse serialization correctly?
Or maybe you can do it better?

Last edited by konsul41 (2020-06-19 15:59:37)

Offline

#2 2020-06-19 15:33:28

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Unserialise array

Please follow the forum rules and don't post such huge pieces of code/json in the forum itself.

Offline

#3 2020-06-19 15:38:56

konsul41
Member
Registered: 2020-05-21
Posts: 24

Re: Unserialise array

But this is greatly reduced, and only what is needed is cut out

Last edited by konsul41 (2020-06-19 15:40:21)

Offline

#4 2020-06-19 22:54:46

Vitaly
Member
From: UAE
Registered: 2017-01-31
Posts: 168
Website

Re: Unserialise array

Probably I misunderstand something, but why not use TKonfiguracja directly as a parameter in your service method? I think it will be transmitted as JSON object without any additional action.

Offline

#5 2020-06-21 07:42:39

konsul41
Member
Registered: 2020-05-21
Posts: 24

Re: Unserialise array

I made collections and works.

Offline

Board footer

Powered by FluxBB