#1 2017-11-28 16:32:27

automacaosamos
Member
Registered: 2015-02-19
Posts: 20

Deserialize an ObjectList with Collection

My Code:

type
  TTestItensVO = class(TCollectionItemAutoCreateFields)
  private
    fitems_id           : Integer;
    fitems_descriptions : RawUTF8;
  published
    property items_id               : Integer    read fitems_id                write fitems_id;
    property Itens_descriptions : RawUTF8 read fitems_descriptions  write fitems_descriptions;
  end;

  TTestItensVOSubVO = class(TInterfacedCollection)
   private
      function GetCollItem(aIndex: Integer): TTestItensVO;
    protected
      class function GetClass: TCollectionItemClass; override;
    public
      property Item[aIndex: Integer]: TTestItensVO read GetCollItem; default;
      function Add: TTestItensVO;
  end;

type
   TTestVO = class
  private
    fcode      : Integer;
    fname     : String;
    fanswers : String;
    ftotal      : Real;
    fitems     : TTestItensVOSubVO;
  published
    property code      : integer                    read fcode        write fcode;
    property name     : string                      read fname      write fname;
    property answers : string                      read fanswers   write fanswers;
    property total      : real                         read ftotal        write ftotal;
    property items     : TTestItensVOSubVO read fitems       write fitems;
    constructor Create;
    destructor Destroy; override;
  end;
.
.
constructor TTestVO.Create;
begin
  items := TTestItensVOSubVO.Create;
end;
destructor TTestVO.Destroy;
begin
  FreeAndNil(fitems);
  inherited;
end;
function TTestItensVOSubVO.Add: TTestItensVO;
begin
  result := TTestItensVO(inherited Add);
end;
class function TTestItensVOSubVO.GetClass: TCollectionItemClass;
begin
  result := TTestItensVO;
end;
function TTestItensVOSubVO.GetCollItem(aIndex: Integer): TTestItensVO;
begin
  result := TTestItensVO(GetItem(aIndex));
end;
.
.
procedure TFormTest.SBObjectListClick(Sender: TObject);
var
  StringJson : RawUTF8;
  ListObj      : TList;
  Itens         : Integer;
  Elements   : Integer;
  TestVO      : TTestVO;
begin
  StringJson :='[{"code": 1,"name": "Margaret","answers": "yes","total": 10,"items": [{"items_id": 1,"items_descriptions": "item1"},{"items_id": 2,"items_descriptions": "item2"}]},' +
               '{"code": 2,"name": "Mary","answers": "no","total": 20,"items":[{"items_id": 3,"items_descriptions": "item3"},{"items_id": 4,"items_descriptions": "item4"}]}]';
  ListObj    := JSONToObjectList(TTestVO,StringJson);
  try
    for Itens := 0 to ListObj.Count -1 do
      begin
        TestVO := ListObj[Itens];
        ShowMessage(TestVO.name);
        for Elements := 0 to TestVO.items.Count -1 do
         ShowMessage('Item: ' + IntToStr(TestVO.items.Item[Elements].items_id) + ' Descriptions: ' + TestVO.items.Item[Elements].Itens_descriptions);
      end;
  finally
    FreeAndNil(ListObj);
  end;
end;
this code error:
First chance exception at $00710079. Exception class $C0000005 with message 'access violation at 0x00710079: read of address 0x00000008'. Process Teste.exe (11100)

Last edited by automacaosamos (2017-11-29 00:54:05)

Offline

#2 2017-11-28 21:17:06

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

Re: Deserialize an ObjectList with Collection

Please follow the forum rules, and don't post direct such big pieces of code in the forum itself.
See https://synopse.info/forum/misc.php?action=rules

Offline

Board footer

Powered by FluxBB