5

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

  • A TSettings = class(TPersistent) for the container
  • Each data to serialize in a published property
  • List of object were TCollection and TCollectionItem types
  • The TJvAppXMLFileStorage component to save everything with the single line :

    JvAppXMLFileStorage.WritePersistent(...);

BUT now, I'm using TObjectList as properties in the TSettings class.
So I drop the TCollection/TCollectionItem in favor of Generics ...
When serializing it, there is no list of items ... I think it's because TObjectList is not from TPersistent.

How can I serialize my TObjectList<> with TJvAppXMLFileStorage ?

TridenT
  • 4,801
  • 1
  • 28
  • 56
  • I've seen this related question, but it's not about TCOllection and TObjectList<> . http://stackoverflow.com/questions/368913/whats-a-good-way-to-serialize-delphi-object-tree-to-xml-using-rtti-and-not-cus – TridenT Sep 17 '12 at 15:01
  • Sounds like an interesting question, but it's a bit unclear what you are doing / asking. – awmross Sep 18 '12 at 00:18
  • 2
    You can use it the same way like in the answer you've linked, simply iterate over your object list and use `JvAppXMLFileStorage1.WritePersistent('', ObjectList.Items[i]);` – TLama Sep 18 '12 at 00:44
  • 1
    This part of JediVCL is derived from RxLib, developed for 16-bit Delphi 1.0. And though JediVCL dropepd support for D1-D5 they still try to remain compatible with D6. So i'd not hope for generics support in JediVCL, twice so in design-time code (at least until IDE itself support generics in D-T). I may suggest you few ways: to implement this feature in JVCL yourself; to try some ORM framework around Spring4Delphi (work not complete yet); try SuperObject or another modern JSON persistent layer; try other XML parsers like Jedi CodeLib, OmniXML, NativeXML in hope that they support generics. – Arioch 'The Sep 18 '12 at 07:12
  • 1
    possible duplicate of [Are there Delphi object serialization libraries with support for Generics?](http://stackoverflow.com/questions/6314451/are-there-delphi-object-serialization-libraries-with-support-for-generics) – TridenT Sep 18 '12 at 13:26

1 Answers1

2

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 !

TridenT
  • 4,801
  • 1
  • 28
  • 56