Summary

TSerializer is the base class type for serializer objects, which can serialize values of various data types into different destination formats, such as Binary, XML or JSON.

Syntax

TSerializer = class abstract

Constructors

 NameDescription
Create 
Top

Methods

 NameDescription
BeginArray(string)

BeginArray method is a low-level method, which allows to serialize array like data manually. Every call to BeginArray method should be matched by a call to NG.Serialization.TSerializer.EndArray method.

BeginArray 
BeginObject

BeginObject method is a low-level method, which allows to serialize object like data manually. Every call to BeginObject method should be matched by a call to NG.Serialization.TSerializer.EndObject method.

BeginObject(string,Boolean)

BeginObject method is a low-level method, which allows to serialize  object like data manually. Every call to BeginObject method should be matched by a call to NG.Serialization.TSerializer.EndObject method.

DoBeginArray 
DoBeginObject 
DoEndArray 
DoEndObject 
DoWrite 
EndArray

EndArray method should be called to finalize array data serialization started by NG.Serialization.TSerializer.BeginArray(string) method. For more information look NG.Serialization.TSerializer.BeginArray(string) method description.

EndObject

EndObject method should be called to finalize object data serialization started by NG.Serialization.TSerializer.BeginObject(string,Boolean) method. For more information look NG.Serialization.TSerializer.BeginObject(string,Boolean) method description.

GetChildTag 
IsRoot 
Null

Null method allows to serialize null or nil like values.

Prop

Prop method allows to specify property name when serializing object properties.

StateError 
Value<T>

Value method allows to serialize typed data value. Both, simple types, such as Integer or string, and complex types, such as records, objects or arrays can be serialized using Value method.

Top

Remarks

Use specialized serializer objects to serialize data values into particular medium. Each descendant of TSerializer base class implements writing into particular medium, such as Xml node, JSON document or data-stream. The examples of specialized serializers are TBinarySerializer and TXmlSerializer.

TSerializer introduces common public methods for serializing values of almost any Delphi data type in formatted manner. TSerializer reflects JSON data model closely; and thus, at low level it provides a way for writing single typed values, objects with named properties and arrays with (unnamed) elements.

Values of any type, such as simple types (like Integer or string), as well as complex types (like classes, records or arrays) can be serialized using single Value method call. For example, if an object need to be serialized, just call Value method passing an object as the argument. Whole object, including values of its public fields and properties will be serialized:

DelphiCopyCode imageCopy Code
S.Value<TMyObject>(obj);

TSerializer class also provides low-level API for formatting object and array data manually. Object like output can be achieved using NG.Serialization.TSerializer.BeginObject(string,Boolean), NG.Serialization.TSerializer.EndObject and NG.Serialization.TSerializer.Prop(string) methods, like this:

DelphiCopyCode imageCopy Code
S.BeginObject('Book', False);
S.Prop('Tittle').Value<string>('Delphi XE Handbook');
S.Prop('Author').Value<string>('Marco Cantu');
S.EndObject;
while array like output should be performed like this:
DelphiCopyCode imageCopy Code
S.BeginArray('Number');
S.Value<Integer>(1);
S.Value<Integer>(1);
S.Value<Integer>(2);
S.Value<Integer>(3);
S.EndArray;

Note that writing object properties requires to use NG.Serialization.TSerializer.Prop(string) method to specify property names, while it should not be used when writing array element. So, the sequence, in which serializer methods are called, should be consistent, otherwise invalid state exception will be thrown.

See Also

Reference