Skip to content

Serializable Pascal Records (RecordUtils.pas)

Glen Kleidon edited this page Sep 29, 2017 · 3 revisions

Pascal Records are extremely useful for working with the Parallel Type Library because they are automatically memory managed. However not being classes, they are more difficult to automatically populate from Streamed Data without using published properties.

The RecordUtils.pas file uses RTTI to automatically serialise and deserialse as value pairs. eg

TMyRecordStatus = (mrsNone, mrsBusy, mrsIdle); 
TMyRecord=Record
  id : integer;
  Name: string;
  isNew : boolean;
  Status : TMyRecordStatus; // enum
end;

WIll be serialised as

id=1
Name=Test1
isNew=True
Status=mrsBusy

Supported Types

Currently only the Data types Integer, String, Boolean and Enumerated type are supported. NESTED Records are not supported at this time. The fact that Records are automatically managed is kind of a double edged sword - it is difficult to create nested record within a function, because the record will be cleaned up as the function ends - ie they will not persist.

Support for Arrays

Yes, arrays are supported. The Above example as an array would be output as:

id[0]=1
Name[0]=Test1
isNew[0]=True
Status[0]=mrsBusy

id[1]=2
Name[1]=Test2
isNew[1]=false
Status[1]=mrsNone

The Data Type and Array count can optionally be included in the data in the following way:

Single Record

RecordType=TMyRecord

Array of Records

RecordType=TMyRecord[]
RecordCount=2

The alternative format where each id is prefixed with the data type is also supported for Parsing (but will not be output in this format). Eg

Single Record

TMyRecord.id=1
TMyRecord.Name=Test1
TMyRecord.isNew=True
TMyRecord.Status=mrsBusy

Array of Records

TMyRecord[0].id=1
TMyRecord[0].Name=Test1
TMyRecord[0].isNew=True
TMyRecord[0].Status=mrsBusy

TMyRecord[1].id=1
TMyRecord[1].Name=Test1
TMyRecord[1].isNew=True
TMyRecord[1].Status=mrsBusy

Line Endings

Internally the string structure is managed as a TStringlist, so the line ending symantics are a consequence of that architecture.

The <CR> (Carriage Return, ASCII-13) cannot be represented as a single line in the TStringlist (by default) and consequently are always converted to <LF> (Line Feed, ASCII-10). So <CRLF> becomes <LF>, <CR> becomes <LF>, <CRLF><LF> becomes <LF><LF>. At this stage, to preserve <CR>, you must pre-process the string representation using an alternate encoding scheme (eg HTML encoding )