Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:23:27+00:00 2026-06-16T04:23:27+00:00

I’m wondering how I can perform serialization of a generic TObjectList<T> container. Basically, I

  • 0

I’m wondering how I can perform serialization of a generic TObjectList<T> container. Basically, I want to store different objects in that list, but all objects will descend from TSerializable, which is defined as follows:

  TSerializable = class abstract(TObject)
  public
    { Public declarations }
    procedure LoadFromStream(const S: TStream); virtual; abstract;
    procedure SaveToStream(const S: TStream); virtual; abstract;
  end;

Now, let’s say I have these classes defined somewhere in my app:

type
    TExampleClass = class(TSerializable)
    private
        { Private declarations }
        FIntProp: Integer;
    public
        { Public declarations }
        constructor Create();

        procedure LoadFromStream(const S: TStream); override;
        procedure SaveToStream(const S: TStream); override;

        property IntProp: Integer read FIntProp write FIntProp;
    end;

    TAnotherExample = class(TSerializable)
    private
        { Private declarations }
        FStringProp: String;
    public
        { Public declarations }
        constructor Create();

        procedure LoadFromStream(const S: TStream); override;
        procedure SaveToStream(const S: TStream); override;

        procedure ReverseStringProp();

        property StringProp: String read FStringProp write FStringProp;
    end;

I’m planning to store such objects in a list:

var
    MS: TMemoryStream;
    SomeList: TObjectList<TSerializable>;
begin
    MS := TMemoryStream.Create();
    SomeList := TObjectList<TSerializable>.Create(True);
    try
        SomeList.Add(TExampleClass.Create());
        SomeList.Add(TAnotherClass.Create());

        TExampleClass(SomeList[0]).IntProp := 1992;
        TAnotherClass(SomeList[1]).StringProp := 'Some value';

        //  Here, a method to serialize the list...
        SerializeList(SomeList, MS);

        //  Clear the list and reset position in the stream.
        SomeList.Clear();
        MS.Seek(0, soFromBeginning);

        //  Unserialize the list.
        UnserializeList(SomeList, MS);

        //  Should display "Some value".
        Writeln(TAnotherClass(SomeList[1]).StringProp);
    finally
        SomeList.Free();
        MS.Free();
    end;
end;

Now, how could I possibly serialize the whole list to stream and then re-create the list from that stream?

What I was thinking about was:

  1. Iterate through the list.
  2. Write each object’s class name to the stream first.
  3. Call SaveToStream() on that object.

But for that approach to work, I would need to create some kind of a class register, which would be some kind of a dictionary to store known classes. It sounds like a good idea, but then I would need to call some RegisterClass() method to add every new class to the dictionary, and I don’t like that way too much.

Is there any other way, or should I just do it the way I proposed?

Thanks a bunch.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:23:28+00:00Added an answer on June 16, 2026 at 4:23 am

    Thank you guys for tips. I have decided to use my own approach, which is probably not the best one, but suits the needs of my small project.

    I thought that someone might be interested in such approach, so I posted it here.

    Basically, what I decided on is to have a base class TSerializable:

    type
      TSerializable = class abstract(TObject)
      public
        { Public declarations }
        procedure LoadFromStream(const S: TStream); virtual; abstract;
        procedure SaveToStream(const S: TStream); virtual; abstract;
      end;
    

    Every descendant class needs to implement LoadFromStream() and SaveToStream() and handle saving to stream separately. It would be probably good to write some generic methods, which would load/save all class properties automatically.

    Then, I have this small class:

    type
      TSerializableList = class(TObjectList<TSerializable>)
      public
        procedure Serialize(const S: TStream);
        procedure UnSerialize(const S: TStream);
      end;
    

    The code is:

    { TSerializableList }
    
    procedure TSerializableList.Serialize(const S: TStream);
    var
      CurrentObj: TSerializable;
      StrLen, StrSize: Integer;
      ClsName: String;
    begin
      S.Write(Self.Count, SizeOf(Integer));
      for CurrentObj in Self do
      begin
        ClsName := CurrentObj.QualifiedClassName();
        StrLen := Length(ClsName);
        StrSize := SizeOf(Char) * StrLen;
    
        S.Write(StrLen, SizeOf(Integer));
        S.Write(StrSize, SizeOf(Integer));
        S.Write(ClsName[1], StrSize);
    
        CurrentObj.SaveToStream(S);
      end;
    end;
    
    procedure TSerializableList.UnSerialize(const S: TStream);
    var
      I, NewIdx, TotalCount, Tmp, Tmp2: Integer;
      ClsName: String;
      Context: TRttiContext;
      RttiType: TRttiInstanceType;
    begin
      Context := TRttiContext.Create();
      try
        S.Read(TotalCount, SizeOf(Integer));
        for I := 0 to TotalCount -1 do
        begin
          S.Read(Tmp, SizeOf(Integer));
          S.Read(Tmp2, SizeOf(Integer));
    
          SetLength(ClsName, Tmp);
          S.Read(ClsName[1], Tmp2);
    
          RttiType := (Context.FindType(ClsName) as TRttiInstanceType);
          if (RttiType <> nil) then
          begin
            NewIdx := Self.Add(TSerializable(RttiType.MetaclassType.Create()));
            Self[NewIdx].LoadFromStream(S);
          end;
        end;
      finally
        Context.Free();
      end;
    end;
    

    Quick and dirty, but works for what I need.

    NOTE
    Since the code uses extended RTTI, it won’t compile in older Delphi versions. Also, you might need to add {$STRONGLINKTYPES ON} in your DPR file or invent some other mechanism, so that linker doesn’t skip your classes (David Heffernan suggest one way here)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.