Deserialization of Object JSON morMot

5

I am trying to deserialize a JSON object with an objectlist, following the examples from the mormot forum does not work, can anyone help me:

Memo.Text := '{"CODIGO":"01","NOME":"CIDO","TELEFONES":[{"DDD":"18","NUMERO":"996237140"},{"DDD":"18","NUMERO":"996237140"}]}';

var
  ClientesVO  : TClientesVO;
  TelefonesVO : TTelefonesVO;
  isValid     : boolean;
begin

  Memo.Text := '{"CODIGO":"01","NOME":"CIDO","TELEFONES":[{"DDD":"18","NUMERO":"996237140"},{"DDD":"18","NUMERO":"996237140"}]}';

  ClientesVO  := TClientesVO.Create;
  TelefonesVO := TTelefonesVO.Create;

  TJSONSerializer.RegisterClassForJSON([TClientesVO,TTelefonesVO]);

  JSONToObject(ClientesVO, @Memo.Text[1], isValid, nil, [j2oIgnoreUnknownProperty]);

  ObjectToJson(TelefonesVO,ClientesVO.TELEFONES.Items[0]);

Classes:

type
  TTelefonesVO = class(TSynPersistent)
  private
    P_DDD    : RawUTF8;
    P_NUMERO : RawUTF8;
  published
    property DDD    : RawUTF8 read P_DDD    write P_DDD;
    property NUMERO : RawUTF8 read P_NUMERO write P_NUMERO;
  end;

type TPhoneVOObjArray = Array of TTelefonesVO;

type
  TClientesVO = class(TSynPersistent)
  private
    P_CODIGO      : RawUTF8;
    P_NOME        : RawUTF8;
    P_TELEFONES   : TPhoneVOObjArray;
  published
    property CODIGO      : RawUTF8          read P_CODIGO     write P_CODIGO;
    property NOME        : RawUTF8          read P_NOME       write P_NOME;
    property TELEFONES   : TPhoneVOObjArray read P_TELEFONES  write P_TELEFONES;
  end;
    
asked by anonymous 29.11.2016 / 14:05

1 answer

2

In order for the framework to recognize its objects, (in this case the phone) it is necessary for the Object and Object List to be of type Collection and CollectionItem .

Code:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  mORMot,
  SynCommons,
  Vcl.StdCtrls,
  System.Generics.Collections;

type

  TTelefone = class(TCollectionItem)
  private
    FDDD: RawUTF8;
    FNumero: RawUTF8;

  published
    property DDD: RawUTF8 read FDDD write FDDD;
    property Numero: RawUTF8 read FNumero write FNumero;

  end;

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

  TPessoa = class(TPersistent)
  private
    FChave: RawUTF8;
    FNome: RawUTF8;
    FTelefone: TListaTelefones;

  published
    property Chave: RawUTF8 read FChave write FChave;
    property Nome: RawUTF8 read FNome write FNome;
    property Telefone: TListaTelefones read FTelefone write FTelefone;

  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Memo2: TMemo;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FObjetoPessoa: TPessoa;
    FObjetoTelefones: TListaTelefones;

    FObjetoPessoaDesserializado: TPessoa;
    FObjetoTelefonesDesserializado: TListaTelefones;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}




procedure TForm1.Button1Click(Sender: TObject);
var
  Telefone: TTelefone;
begin

  FObjetoPessoa.Chave := '1';
  FObjetoPessoa.Nome  := 'Victor';

  FObjetoTelefones.Add;
  FObjetoTelefones.Last.DDD    := '49';
  FObjetoTelefones.Last.Numero := '99999999';

  FObjetoTelefones.Add;
  FObjetoTelefones.Last.DDD    := '47';
  FObjetoTelefones.Last.Numero := '77777777';

  FObjetoPessoa.Telefone := FObjetoTelefones;

  Memo1.Lines.Add(ObjectToJSON(FObjetoPessoa));

end;



procedure TForm1.Button2Click(Sender: TObject);
var
  isValid: Boolean;
  SyntaxErrorPointer: PUTF8Char;
  JSON: RawUTF8;
begin
  JSON := RawUTF8(Memo2.Lines.Text);
  SyntaxErrorPointer := JSONToObject(FObjetoPessoaDesserializado, @JSON[1], isValid);

end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  FObjetoPessoa    := TPessoa.Create;
  FObjetoTelefones := TListaTelefones.Create(TTelefone);

  FObjetoPessoaDesserializado    := TPessoa.Create;
  FObjetoTelefonesDesserializado := TListaTelefones.Create(TTelefone);
end;



procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(FObjetoTelefonesDesserializado);
  FreeAndNil(FObjetoPessoaDesserializado);

  FreeAndNil(FObjetoTelefones);
  FreeAndNil(FObjetoPessoa);

end;

{ TListaTelefones }



function TListaTelefones.Add: TTelefone;
begin
  Result := inherited Add as TTelefone;
end;



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



function TListaTelefones.Last: TTelefone;
begin
  Result := Item[Count - 1];
end;

end.

Bbs: This structure was a little strange for me. Instead you would opt for the framework Delphi-OOP because I can use it with ObjectList<T> Generics.

    
30.11.2016 / 21:09