DeHL, Delphi 2010 and Serialization

DeHLA few months have passed and I did not release a new version of DeHL yet. No, it’s not dead. I’ve just been busy with a delicate new feature — Serialization. This post will demonstrate the new capabilities of DeHL it’s advantages and and shortcomings.

But first — since the new releases will focus mostly on serialization and related stuff, I decided to drop Delphi 2009 support. It made no sense to support 2009 for future versions since no essential changes are made to the prior code. You can still use 0.7 release in Delphi 2009.

Back to serialization. The following list describes the changes that went into the new version:

  • In order to support serialization, DeHL’s type system was extended to support Serialize and Deserialize methods. Each type class (that describes a type in Delphi) now knows how to serialize values of the type it manages.
  • A new unit named DeHL.Serialization was added. It contains the base definitions of types used by the type system for serialization.
  • TPointerType, TRecordType<T>, TArrayType<T>, etc. were added for simplified type handling. The old method was a mix-up of Delphi 2009 and Delphi 2010 RTTI specifics (which have some essential differences in my case).
  • All classes can now implement ISerializable interface. The TClassType<T> detects whether this interface is implemented by the object and uses it for serialization (no, reference counting is not touched).
  • DeHL.Serialization.Abstract contains the semi-implementation of a “serializer” and it’s context. It is used by specific serializers.
  • DeHL.Serialization.XML defines the TXMLSerializer<T> which can be used to serialize/deserialize into XML nodes (uses TXMLDocument). Supports it’s own set of attributes (such as XmlRoot, XmlElement, etc.).
  • DeHL.Serialization.Ini defines the TIniSerializer<T> that you can use to serialize/deserialize type into Ini files or registry (through RTL’s TRegIniFile).
  • Most DeHL types (such as Nullable<T>TFixedArray<T>, BigInteger, etc.) provide their own serialization and deserialization methods.
  • All Enex collections (except a few that can’t actually) can be serialized and deserialized. They implement a custom serialization and deserialization technique through ISerializable.

Enough talk, a mandatory example:

  [XmlRoot('Testing', '')]
  TTest = class
    { Pointer to self }
    FSelf: TObject; 
    {A set of format settings }
    FFormatSettings: TFormatSettings;

    { And internal record }
    FInternal: record
      { Force the field to be an attribute of FInternal }
      FOne: Integer;

      { Force this element to have same name but other namespace }
      [XmlElement('Value', '')]
      FTwo: String;

    FListOfDoubles: TList<Double>;
  LDocument: IXMLDocument;
  LXMLSerializer: TXMLSerializer<TTest>;
  LOutInst, LInInst: TTest;
  CoInitializeEx(nil, 0);

  { Initialize the test object }
  LOutInst := TTest.Create;
  LOutInst.FSelf := LOutInst;
  GetLocaleFormatSettings(GetThreadLocale(), LOutInst.FFormatSettings);
  LOutInst.FInternal.FOne := 1;
  LOutInst.FInternal.FTwo := '2 - Two';
  LOutInst.FListOfDoubles := TList<Double>.Create();

  { Create the serializer and an XML document }
  LXMLSerializer := TXMLSerializer<TTest>.Create();
  LDocument := TXMLDocument.Create(nil);

  { Set the options }
  LDocument.Active := true;
  LDocument.Options := LDocument.Options + [doNodeAutoIndent];

  { Force fields to elements by default }
  LXMLSerializer.DefaultFieldsToTags := true;

  { Serialize the structure }
  LXMLSerializer.Serialize(LOutInst, LDocument.Node);

  { Serialize the structure }
  LXMLSerializer.Deserialize(LInInst, LDocument.Node);

  { Cleanup }

The XML file generated by this code looks like this (INI looks uglier):

<Testing xmlns="" xmlns:xsd="" xmlns:xsi="" xmlns:DeHL="" xmlns:NS1="">
  <PointerToSelf DeHL:ref="Testing"/>
    <LongDateFormat>dddd, MMMM dd, yyyy</LongDateFormat>
    <ShortTimeFormat>h:mm AMPM</ShortTimeFormat>
    <LongTimeFormat>h:mm:ss AMPM</LongTimeFormat>
  <FInternal Value="1">
    <NS1:Value>2 - Two</NS1:Value>

On the first serialized/deserialized value, serializers build up a sort of an internal “object graph” and gathers all information about the data being serialized. The next uses of the same serializer instance yield an 10x performance gain since there is no need to rebuild all the information from scratch. I am still working on more optimizations that could give greater speed boost.

P.S. I can’t show the contents of the deserialized object here so you’ll have to take my word for it.

Note. This is just a preview of what is going on in the trunk. No version is released since I have to iron out the last problems and write the missing unit tests.

You May Also Like

About the Author: Alexandru Ciobanu


  1. I have been watching your commits, and I like your implementation.
    I have something similar, but really serves to handle things in an identical manner to .NET to the XmlSerializer.

  2. @Robert. Thanks.

    My engine is only for deep serialization. Indeed my XML serialization is not .Net-compatible. But you can use it in a somewhat similar way.

  3. We serialize objects to/from DFM quite a bit. We’re able to simply use Delphi’s internal engine, using DefineProperties to provide overrides as necessary. I like the DFM’s format much better than either INI or XML. XML is way too verbose.

    One disadvantage was not being able to treat our serialized object as a data type in SQL Server 2005. But we don’t need this often and wrote a function to extract values from a serialized DFM using string functions.

  4. In the example you are serializing a TList defined as:
    FListOfDoubles: TList;

    Could you outline how to serialize a TList, that contains objects such as this ?
    FMyTList: TList;

  5. In the above it should read:

    FListOfDoubles: TList(Double)


    FMyTList: TList(TMyObject)

    The commenting system does not like the less- and greater than symbols.

  6. It works now after including DeHL.Collections.List instead of including Generics.Collections. The TList definition must be the one from the DeHL framework.

    I really like how easy it is to save a complex object now.

  7. @Bo Sorensen
    Yeah, only DeHL collections are serializable. I was going to add a few modules to be able to seamlessly serialize other RTL-native collections (not TList since it contains opaque pointers).

  8. I had one problem with deserialisation.

    I had this :
    TPart1 = Class
    field1: string;

    TPart = Class
    part1: TPart1;

    I serialize and deserailize : all work fine.

    But now I want to add TPart2 to TPart like this :

    TPart1 = Class
    field1: string;

    TPart2 = Class
    field2: string;

    TPart = Class
    part1: TPart1;
    part2: TPart2;

    And I’d like to deserialize from TPart XML who was serialized with old TPart (only TPart1) and it fail. Is there a way to make this ?

  9. You should be able to — by annotating the “part2” field with [XmlNullable]. This will tell the XML serializer to set the “part2” field to NIL if it does not have an XML node.

    Note that while this is possible it’s not recommended. Deserialized content should be serialized back to the original types otherwise “bad things” may happen.

  10. @alex

    and what is the way to add a new field in Part2 like this

    TPart2 = Class
    field2: string;
    newfield2: Boolean;

  11. Hi!

    I love your library, especially enex and serialization. One thing though: How exactly do I exclude class members from being serialized?

    Thanks for your help, in advance!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.