Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save/Load TObject(TPersistent) to XML

everybody.

I'm trying to save my class:

TA= class(TPersistent)
private
    FItems: TObjectList<TB>;

    FOnChanged: TNotifyEvent;
public
    constructor Create;
    destructor Destroy; override;
    ...
    procedure Delete(Index: Integer);
    procedure Clear;
    procedure SaveToFile(const FileName: string);
    ...
    property OnChanged: TNotifyEvent read FOnChanged write FOnChanged;
end;

to file using the following code:

var
  Storage: TJvAppXMLFileStorage;
begin
  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Self);
    Storage.Xml.SaveToFile(FileName);
  finally
    Storage.Free;
  end;

but file is always empty.

What am I doing the wrong way?

like image 328
Keeper Avatar asked Sep 10 '26 01:09

Keeper


1 Answers

It looks like TJvCustomAppStorage does not support Generics in properties. The code makes no use of extended RTTI and the call to TJvCustomAppStorage.GetPropCount returns 0.

This leads to another question - Are there Delphi object serialization libraries with support for Generics??

My test code:

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Generics.Collections, JvAppXmlStorage;
type

  TA = class(TPersistent)
  private
    FItems: TObjectList<TPersistent>;
  public
    constructor Create;
  published
    property
      Items: TObjectList < TPersistent > read FItems write FItems;
  end;

  { TA }

constructor TA.Create;
begin
  FItems := TObjectList<TPersistent>.Create;
end;

var
  Storage: TJvAppXMLFileStorage;
  Test: TA;
begin
  Test := TA.Create;

  Test.Items.Add(TPersistent.Create);

  Storage := TJvAppXMLFileStorage.Create(nil);
  try
    Storage.WritePersistent('', Test);
    WriteLn(Storage.Xml.SaveToString);
    ReadLn;
  finally
    Storage.Free;
  end;

end.
like image 134
mjn Avatar answered Sep 12 '26 19:09

mjn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!