Problems with Serialize Delphi - tkPointer

Asked

Viewed 554 times

1

Good morning, I have a problem trying to use the function:

TJson.ObjectToJsonString( Pedido )

Da Unit REST.Json

The object I’m trying to convert to Json is as follows:

unit class_regjson;

interface

uses
   Classes;

type

   TItemConsultaCep = class(TCollectionItem)
   private
     FLogradouro  : String;
     FComplemento : String;
     FBairro      : String;
     FCidade      : String;
     FUF          : String;
     FUnidade     : String;
   published
     property LOGRADOURO  : String  read FLogradouro   write FLogradouro;
     property COMPLEMENTO : String  read FComplemento  write FComplemento;
     property BAIRRO      : String  read FBairro       write FBairro;
     property CIDADE      : String  read FCidade       write FCidade;
     property UF          : String  read FUF           write FUF;
     property UNIDADE     : String  read FUnidade      write FUnidade;
   end;

   TConsultaCep = class(TCollection)
   private
      function GetItem(AIndex: Integer): TItemConsultaCep;
   public
      count: Integer;
      function Add: TItemConsultaCep;
      property Item[Index: Integer]: TItemConsultaCep read GetItem;
      function Last: TItemConsultaCep;
   end;

   TItemEndereco = class(TCollectionItem)
   private
      FReferencia  : String;
      FComplemento : String;
      FBairro      : String;
      FCidade      : String;
      FNumero      : Integer;
      FCep         : String;
      FEndereco    : String;
      FEstado      : String;
      FConsultaCep : TConsultaCep;
   published
      property REFERENCIA   : String       read FReferencia  write FReferencia;
      property COMPLEMENTO  : String       read FComplemento write FComplemento;
      property BAIRRO       : String       read FBairro      write FBairro;
      property CIDADE       : String       read FCidade      write FCidade;
      property NUMERO       : Integer      read FNumero      write FNumero;
      property CEP          : String       read FCep         write FCep;
      property ENDERECO     : String       read FEndereco    write FEndereco;
      property ESTADO       : String       read FEstado      write FEstado;
      property CONSULTA_CEP : TConsultaCep read FConsultaCep write FConsultaCep;
   end;

   TEndereco = class(TCollection)
   private
      function GetItem(AIndex: Integer): TItemEndereco;
   public
      count : Integer;
      function Add: TItemEndereco;
      property Item[Index: Integer]: TItemEndereco read GetItem;
      function Last: TItemEndereco;
   end;

   TPedido = class(TPersistent)
   private
      FCodWeb     : Integer;
      FReferencia : String;
      FStatus     : Integer;
      FPheTipo    : String;
      FPheMin     : Integer;
      FPheHora    : String;

      FEndereco   : TEndereco;
   public
      procedure Clear;
      procedure Assign (APersistent: TPersistent) ; override;
   published
      property CODIGO_WEB : Integer     read FCodWeb      write FCodWeb;
      property REFERENCIA : String      read FReferencia  write FReferencia;
      property STATUS     : Integer     read FStatus      write FStatus;
      property PHE_TIPO   : String      read FPheTipo     write FPheTipo;
      property PHE_MIN    : Integer     read FPheMin      write FPheMin;
      property PHE_HOR    : String      read FPheHora     write FPheHora;
      property ENDERECO   : TEndereco   read FEndereco    write FEndereco;
   end;

implementation

   procedure TPedido.Assign(APersistent: TPersistent) ;
   begin
      if APersistent is TPedido then
      begin
         CODIGO_WEB := TPedido(APersistent).CODIGO_WEB;
         REFERENCIA := TPedido(APersistent).REFERENCIA;
         STATUS     := TPedido(APersistent).STATUS;
         PHE_TIPO   := TPedido(APersistent).PHE_TIPO;
         PHE_MIN    := TPedido(APersistent).PHE_MIN;
         PHE_HOR    := TPedido(APersistent).PHE_HOR;
         ENDERECO   := TPedido(APersistent).ENDERECO;
      end
      else
         inherited Assign (APersistent);
   end;

   procedure TPedido.Clear;
   begin
      CODIGO_WEB := 0;
      REFERENCIA := '';
      STATUS     := 0;
      PHE_TIPO   := '';
      PHE_MIN    := 0;
      PHE_HOR    := '';
      ENDERECO   := TEndereco.Create(TItemEndereco);
   end;

   function TEndereco.Add: TItemEndereco;
   var
      itemEndereco: TItemEndereco;
   begin
      itemEndereco := inherited Add as TItemEndereco;
      count := count + 1;
      itemEndereco.CONSULTA_CEP := TConsultaCep.Create(TItemConsultaCep);
      Result := itemEndereco;
   end;

   function TEndereco.GetItem(AIndex: Integer): TItemEndereco;
   begin
     Result := inherited Items[AIndex] as TItemEndereco;
   end;

   function TEndereco.Last: TItemEndereco;
   begin
     Result := Item[Count - 1];
   end;

   function TConsultaCep.Add: TItemConsultaCep;
   begin
      count := count + 1;
      Result := inherited Add as TItemConsultaCep;
   end;

   function TConsultaCep.GetItem(AIndex: Integer): TItemConsultaCep;
   begin
     Result := inherited Items[AIndex] as TItemConsultaCep;
   end;

   function TConsultaCep.Last: TItemConsultaCep;
   begin
     Result := Item[Count - 1];
   end;

end.

When you go through the conversion line I get the following error:

Internal: tkPointer is not Currently supported

Could someone help me?

INFORMATION Delphi 10.3

COMPLETION OF THE OBJECT

  Pedido := TPedido.Create;

  try

     Pedido.Clear;

     Pedido.CODIGO_WEB := 515;
     Pedido.REFERENCIA := '3917635439695030';
     Pedido.STATUS     := 0;
     Pedido.PHE_TIPO   := '';
     Pedido.PHE_MIN    := 0;
     Pedido.PHE_HOR    := '';

     Pedido.ENDERECO.Add;
     Pedido.ENDERECO.Last.REFERENCIA  := '';
     Pedido.ENDERECO.Last.COMPLEMENTO := '';
     Pedido.ENDERECO.Last.BAIRRO      := 'OUTROS';
     Pedido.ENDERECO.Last.NUMERO      := 100;
     Pedido.ENDERECO.Last.CEP         := '12345-678';
     Pedido.ENDERECO.Last.ENDERECO    := 'R TESTE';
     Pedido.ENDERECO.Last.ESTADO      := 'AC';

     Pedido.ENDERECO.Last.CONSULTA_CEP.Add;
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.LOGRADOURO  :=  '';
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.COMPLEMENTO :=  '';
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.BAIRRO      :=  '';
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.CIDADE      :=  '';
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.UF          :=  '';
     Pedido.ENDERECO.Last.CONSULTA_CEP.Last.UNIDADE     :=  '';

     //json := TJson


     Memo1.Lines.Text := TJson.ObjectToJsonString( Pedido );

  finally

     Pedido.Free;

  end;

1 answer

1

Exchange your ENDERECO property, by TARRAY, add the creation of the array in the constructor, and Destroy its cleaning

Browser other questions tagged

You are not signed in. Login or sign up in order to post.