Thursday, March 13, 2008

Object Serialization in Delphi - Part II

This is a unit based on JVCL’s JvgXMLSerializer (from old Globus library). It has some basic classes used for object serialization:

unit XMLSerializer;

interface

uses
SysUtils, Classes, JvgXMLSerializer;

type
TXMLSerializer = class(TJvgXMLSerializer)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
procedure Serialize(Component: TObject; var s: string); overload;
procedure DeSerialize(Component: TObject; s: string); overload;
published
{ Published declarations }
end;

TSerializable =
class(TPersistent)
public
{ Public declarations }
function Serialize: string;
procedure DeSerialize(s: string);
end;

implementation

{ TXMLSerializer }

procedure TXMLSerializer.DeSerialize(Component: TObject; s: string);
var
Stream: TStringStream;
begin
Stream := TStringStream.Create(s);
try
DeSerialize(Component, Stream);
finally
Stream.Free;
end;
end;

procedure TXMLSerializer.Serialize(Component: TObject; var s: string);
var
Stream: TStringStream;
begin
Stream := TStringStream.Create('');
try
Serialize(Component, Stream);
s := Stream.DataString;
finally
Stream.Free;
end;
end;

{ TSerializable }

procedure TSerializable.DeSerialize(s: string);
var
Serializer: TXMLSerializer;
begin
Serializer := TXMLSerializer.Create(nil);
try
Serializer.DeSerialize(Self, s);
finally
Serializer.Free;
end;
end;

function TSerializable.Serialize: string;
var
Serializer: TXMLSerializer;
begin
Serializer := TXMLSerializer.Create(nil);
try
Serializer.Serialize(Self, Result);
finally
Serializer.Free;
end;
end;

end.



The classes declared are:

TXMLSerializer
: my direct descendant of TJvgXMLSerializer. It implements two new methods:
- Serialize: serialize an instance directly to a string, without the necessity of creating a TStream;
- DeSerialize: receive a string as a parameter containing the XML and deserialize the instance.

TSerializable: a TPersistent descendant that implements the Serialize and DeSerialize methods too.
They both work to serialize and deserialize itself.
All my "serializable" classes are direct descendants of TSerializable.

I've modified the original JVCL unit TvgXMLSerializer (using latest version 3.33) to make it compatible with VCL.NET (using BDS 2006).
Using that unit I can create serializable classes that can be shared - and used - among Win32 and .NET modules. Not only creating single sorced projects, but I can pass a XML to a ASP.NET WebService from my Win32 consumer client, in an easy and convinient way.
More about it later. ;-)

4 comments:

Anonymous said...

Hi Alexandre - do you have your modified class publicly available?

Thanks, Bob Devine

Alexandre Caldas Machado said...

Hi Bob, thanks for your interest.

Please send me an email to alex7691 at gmail dot com.

Best regards

Daniel said...

Caro Alexandre, você possui algum tipo de material (apostila) sobre streams e serialização em delphi? Se sim, pode ser distribuído ou é material com copyright?

Grato!

Unknown said...

Exatamente o que eu procurava!
:-)
[ ]s