How to serialize Delphi type TObjectList <TMyClass> for XML using TJvAppXMLFileStorage?

Previously, to save the settings of some applications, I used:

  • A TSettings = class(TPersistent) for the container
  • Each serialization information in a published property
  • List of TCollection Objects and TCollectionItem Type
  • TJvAppXMLFileStorage component for saving in only one line:

    JvAppXMLFileStorage.WritePersistent (...);

BUT , now I use TObjectList as properties of the TSettings class. So I am throwing TCollection / TCollectionItem in favor of Generics ...
There is no list of elements when it is serialized ... I think this is because TObjectList is not from TPersistent.

How can I serialize my TObjectList <> using TJvAppXMLFileStorage ?

+4
source share
1 answer

I have successfully serialized my generic list with a few lines of code by calling JvAppXMLFileStorage.WriteList .

First, this is how I serialized the list . The WriteGenericsObjectListItem<TMyClass> method is described in detail below.

 JvAppXMLFileStorage.WriteList('mylist',TObject(MyGenericList), MyGenericList.Count, WriteGenericsObjectListItem<TMyClass>); 

Then I just need to determine how to serialize each item in the general list. To do this, I created a general method:

 procedure TMySerializer.WriteGenericsObjectListItem<T>(Sender: TJvCustomAppStorage; const Path: string; const List: TObject; const Index: Integer; const ItemName: string); begin if(List is TObjectList<T>) then if Assigned(TObjectList<T>(List)[Index]) then Sender.WritePersistent(Sender.ConcatPaths([Path, Sender.ItemNameIndexPath (ItemName, Index)]), TPersistent(TObjectList<T>(List)[Index])); end; 

What is it!
I do not change the JCL / JVCL code, I just add them to my program.
I think I will send the patch to the JCL / JVCL team to add compatibility with all Generics containers.

Hope this helps you!

+2
source

All Articles