Deserialization of Object JSON morMot

Asked

Viewed 739 times

5

I am trying to deserialize a JSON Object with an objectlist, following the example of the forum mormot does not work, someone can 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;
  • I did some tests, and it worked out nice here for me. But mORMot seems to have some secrets as to the class statement. I could post the declaration code of your classes (Vos)?

  • I’m using Syncrossplatformjson to deserialize

  • Look, I’ve put together an example, and it works almost perfectly. I’m just not being able to deserialize the Colletctionitems from my JSON. What I noticed about mORMot, is that, they have some peculiarities in the creations of the classes. For example, classes with objects need to be inherited from the Collection and Collectionitem type. I had never messed with the serial version of mORMot (I use it only for REST client/server applications and as a tool for ORM). What is the exact problem you are having ?

  • I just looked at your class, and you’re not using the guys recommended by mORMot. I’ll answer with the example I have to see if it helps you a little

  • Cool, and thank you the I also use the Rest have an application with Delphi and Extjs 6.01 from everything works perfect. What I want is to deserialize Collectionitem, and I can’t

  • hoje o collectionItem eu uso uma variavel RawUTF8 pra mandar os itens o ExtJs entende, se uso no TClientesVO property TELEFONES : TList read P_TELEFONES write P_TELEFONES; no ObjectToJSON funciona perfeito, agora o problema é no JSONToObject ele dá erro

Show 1 more comment

1 answer

2

In order for the framework to recognize its objects, (in this case the phone) it is necessary that the Object and Object List are of the 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 bit strange for me. Instead I would choose the framework Delphi-OOP because I can use it with ObjectList<T> Generics.

  • truth. thanks for the help...

  • One thing I realized now in your code, which doesn’t work for me, is to use the direct memo pointer as a parameter in Jsontoobject. Create a Rawutf8 variable, and assign the JSON inside the memo to this guy, and in the passage, use this new variable, as in this example: @JSON[1]

  • It worked using @JSON[1] but I still can’t access the phones I tried to adjust but it didn’t work

Browser other questions tagged

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