It has been a very lean and easy option of .Net to able serialize/deserialize any serializable object instance.
Only closest option in Delphi is to stream the component using WriteComponent/WriteComponentRes of TStream/TWriter (used for Form storage as DFM, for example). It can be then read back using appropriate counterpart ReadComponent/ReadComponentRes.
Depend on your situation, simply calling .Assign method would work if you are coping data from one instance to another. But it only works between inherited classed which know about each other.
Can we find there something which will allow to pass objects states in more readable format?
There is a very powerful infrastructure available to do full serialization without knowing underlying class structure – RTTI (Run-time Type Information).
All functions we would be looking at are defined in TypInfo.pas unit.
First thing first. To be able to work within RTTI, you need to operate on the object which has published and public properties.
function GetPropList(TypeInfo: PTypeInfo; out APropList: PPropList): integer;
A function will return number and reference to the list (array) of properties published (public and published) by the class (VMT information). List will also include published methods. Simply walking through it will give you an access to property/method information.
In our example we are not interested in the published methods, so lets filter it out:
var i: integer; lPropInfo: PPropInfo; lPropCount: integer; lPropList: PPropList; lPropType: PPTypeInfo; begin lPropCount := GetPropList(PTypeInfo(AObject.ClassInfo), lPropList); for i := 0 to lPropCount - 1 do begin lPropInfo := lPropList^[i]; lPropType := lPropInfo^.PropType; if lPropType^.Kind = tkMethod then Continue; // ... processing of the properties ... end; end;
What other Kind of information is present in the list? Bellow is full definition of the type
type TTypeKind = (tkUnknown, tkInteger, tkChar, tkEnumeration, tkFloat, tkString, tkSet, tkClass, tkMethod, tkWChar, tkLString, tkWString, tkVariant, tkArray, tkRecord, tkInterface, tkInt64, tkDynArray);
Now you are ready to get or set the value of the property:
function GetPropValue(Instance: TObject; const PropName: string; PreferStrings: Boolean = True): Variant;
procedure SetPropValue(Instance: TObject; const PropName: string; const Value: Variant);
And now you are ready to serialize your object, store it, load definition and deserialize an object back. And it could be not necessarily the same object.
You would have to loop through the properties, read the values, and store it as an XML for later use.
I am not going to go through all specific details in this post, instead, you can find a full code for XML Class serializer here.
21 Comments
Gabriel · Mar 13, 2025 at 10:32
I think full automatic serialization of complex objects is hard to achieve (because of issues like private fields). However, manual serialization is quite easy. Plus it gives you much better control over the process.
I have an example (compilable code) here: https://gabrielmoraru.com/saving-an-object-to-disk-file/
I also have a (free) library that helps you with the serialization. It offers primary functionality such as file signature (aka magic number) and versioning.