How to serialize Delphi TObjectList type to XML with TJvAppXMLFileStorage?

后端 未结 1 552
故里飘歌
故里飘歌 2021-01-13 01:46

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

  • A TSettings = class(TPersistent) for the container
  • Each data to serialize
相关标签:
1条回答
  • 2021-01-13 02:19

    I've successfuly serialize my generic list with few lines of code by calling JvAppXMLFileStorage.WriteList.

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

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

    Then, I just need to define how to serialize each item of the generic list. For this, I've created a generic 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;
    

    That's it!
    I haven't modify JCL/JVCL code, only add these to my program.
    I think I will submit a patch to JCL/JVCL team to add the compatibility with all Generics containers.

    I hope this can help you !

    0 讨论(0)
提交回复
热议问题