Delphi Serialization Libraries (win32)

Are there Delphi serialization libraries capable of serializing records and record arrays instead of classes?

+5
serialization delphi
source share
4 answers

@Max you can use the TJvAppXMLFileStorage component from JEDI to serialize a record or array of records.

you can use a procedure called WriteBinary to store data and ReadBinary to read.

Unfortunately, there is not much documentation for this component, so here you have a very simple example for storing one record (for an array of records, you can easily change this source code).

Record structure

 type MyRecord= record Field1 : Integer; Field2 : Double; Field3 : String[20]; Field4 : String[20]; end; 

Save Record

 Procedure SaveMyRecord(Rec : MyRecord); var MyStore: TJvAppXMLFileStorage; begin MyStore:= TJvAppXMLFileStorage.Create(nil); try MyStore.FileName:='C:\temp\record.xml'; //this component supports store multiples objects to the same file, so you need use an identifier for you particular object, in this case i'm use the Dummy name. MyStore.WriteBinary('Dummy', @Rec,sizeof(Rec)); MyStore.Xml.SaveToFile(MyStore.FileName); finally MyStore.Free; end; end; 

this procedure creates an XML file like this, the data is encoded in hexadecimal format.

 <?xml version="1.0" encoding="iso-8859-1"?> <Configuration> <Dummy>84030000000000003333333333331F400D737472696E6720746573742031000000000000000D737472696E672074657374203200000000000000000000000000</Dummy> </Configuration> 

Read saved data

 Procedure LoadMyRecord(var Rec : MyRecord); var MyStore: TJvAppXMLFileStorage; begin MyStore:= TJvAppXMLFileStorage.Create(nil); try MyStore.FileName:='C:\temp\record.xml';//point to the same file MyStore.Xml.LoadFromFile(MyStore.FileName); //load the file MyStore.ReadBinary('Dummy', @Rec,sizeof(Rec));//use the Dummy identifier and pass the record as an pointer finally MyStore.Free; end; end; 

Check out this complete project (tested in Delphi 7)

 program ProjectPersistRecord; {$APPTYPE CONSOLE} uses SysUtils, JvAppXMLStorage; type MyRecord= record Field1 : Integer; Field2 : Double; Field3 : String[20]; Field4 : String[20]; end; Procedure SaveMyRecord(Rec : MyRecord); var MyStore: TJvAppXMLFileStorage; begin MyStore:= TJvAppXMLFileStorage.Create(nil); try MyStore.FileName:='C:\temp\record.xml'; MyStore.WriteBinary('Dummy', @Rec,sizeof(Rec)); MyStore.Xml.SaveToFile(MyStore.FileName); finally MyStore.Free; end; end; Procedure LoadMyRecord(var Rec : MyRecord); var MyStore: TJvAppXMLFileStorage; begin MyStore:= TJvAppXMLFileStorage.Create(nil); try MyStore.FileName:='C:\temp\record.xml'; MyStore.Xml.LoadFromFile(MyStore.FileName); MyStore.ReadBinary('Dummy', @Rec,sizeof(Rec)); finally MyStore.Free; end; end; Var Rec : MyRecord; begin //Fill the record Rec.Field1:=900; Rec.Field2:=7.8; Rec.Field3:='string test 1'; Rec.Field4:='string test 2'; SaveMyRecord(Rec); //save the record FillChar(Rec,SizeOf(Rec),#0); //clear the record variable LoadMyRecord(Rec);//restire the record data //show the loaded data Writeln(rec.field1); Writeln(rec.field2); Writeln(rec.field3); Writeln(rec.field4); Readln; end. 
+6
source share

If you have Delphi 2010, you can take a look at DeHL . It contains a serialization library that can handle almost any type of data.

+3
source share

Another solution that works with Delphi 5 to XE2 is available in one of our OpenSource blocks .

In fact, it implements:

  • Some low-level RTTI functions for processing record types: RecordEquals, RecordSave, RecordSaveLength, RecordLoad ;
  • A special TDynArray object that is a wrapper around any dynamic array, capable of detecting TList-like methods around any dynamic array, even containing records, strings, or other dynamic arrays. It is able to serialize any dynamic array.

Serialization uses an optimized binary format and can save and load any record or dynamic array like RawByteString . You also have JSON serialization at hand, including a custom layout - see standard serialization of JSON records .

+3
source share

Being a record, if you have no properties, I don’t think you are still ahead, trying to use any persistence infrastructure (like DeHL).

The accepted answer, while technically correct, is questionable in a real utility and has many long-term support scenarios - a nightmare if you use it in production. DO NOT DO IT.

If your program is just a little ad-hoc'ery, can I humbly assume that you are downloading it in the old school with a “recording file”, a classic Turbo Pascal technique that still works.

0
source share

All Articles