Here you go — DeHL 0.8

DeHL

This is going to be a short one. After months of no releases, here it is: DeHL 0.8 (see changelog for the list of changes on this release). As I mentioned previously, this release will only work on Delphi 2010, since the number of changes required to support serialization was quite big.

I must confess, this is the first time in my programming career that I had to interact with serialization (excluding the times when the frameworks do that for you). I had to learn quite a bit and changed my internal design three times.

So, here’s an example how to serialize a TFormatSettings structure into binary format:

var
  LBinFile: TFileStream;
  LBinSerializer: TBinarySerializer<TFormatSettings>;
  LSettings: TFormatSettings;
begin
  { Obtain current thread's format settings }
  GetFormatSettings(GetThreadLocale(), LSettings);

  { Create a serializer and a file stream }
  LBinSerializer := TBinarySerializer<TFormatSettings>.Create();
  LBinFile := TFileStream.Create('dump_obj.bin', fmCreate);

  { Serialize the structure }
  try
    LBinSerializer.Serialize(LSettings, LBinFile);
  finally
    LBinFile.Free;
    LBinSerializer.Free;
  end;
end;

… and here’s an example how to deserialize it:

var
  LBinFile: TFileStream;
  LBinSerializer: TBinarySerializer<TFormatSettings>;
  LSettings: TFormatSettings;
begin
  { Create a serializer and a file stream }
  LBinSerializer := TBinarySerializer<TFormatSettings>.Create();
  LBinFile := TFileStream.Create('dump_obj.bin', fmOpenRead);

  { Deserialize the structure }
  try
    LBinSerializer.Deserialize(LSettings, LBinFile);
  finally
    LBinFile.Free;
    LBinSerializer.Free;
  end;
end;

Note: I have written quite a few unit tests to support the new changes, but most certainly there are hidden bugs. If you find one please report it here.

Other Note: I exhausted my idea jar regarding new features. If you have an idea please do not hesitate to drop me a comment or an email.

Have Fun!

You May Also Like

About the Author: Alexandru Ciobanu

7 Comments

  1. Very interesting.
    How do you handle graph of object?
    Suppose that a object (O) to be serialized contains a reference to a contained object (C).
    If c has a property that refers to its container object, how it will be serialized?

    Eg.
    var
    O : TClassO;

    O.c := TClassC.Create;

  2. Each object (actually “referenced type” as there can be pointers to records or dyn arrays) get an unique ID that is used when the same ref is found the second time.

  3. I’m getting the following error: “The entity FItemClass of type TCollectionItemClass does not define any serialization/deserialization method!”. I tested my really big settings class on it, but as you see in the error – it don’t works. Furthermore i miss detailed information and samples what your code can and cannot do. For example i use many TCollection’s to save my settings, looks like your code cannot handle this 🙁 maybe you can improve this for the future or maybe i do something wrong.

  4. The serializer cannot handle “opaque” data types. For example you cannot serialize a TList class since it contains pointers and there is no knowing what’s behind that pointer. In the same manner it seems that FItemClass is a TCollectionItemClass (TClass) type and that is not handled yet (forgot actually). But in any case there is a small chance you can get some complicated data type and serialize it successfully, and there’s no need to actually.

  5. Hi, I have the following problem, when i try to deserialize, it trows an exception of RTTI, at this line LType := FContext.FindType(AName); on method GetClassByQualifiedName, I use packages loaded in run time, could you help me?!, ps: I tried to serialize an object, and it have only strings and integers on it.

Leave a Reply to Anonymous Cancel reply

Your email address will not be published.