Summary

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.

Syntax

procedure BeginObject(const AType: string;
                          AWriteType: Boolean = True); overload;

Parameters

AType

Type: string

Specifies class name of the object value. Read Remarks section for more description. If the value of AWriteType parameter is False, then the parameter value is not used.
AWriteType

Type: Boolean

Specifies, whether the AType class name is required for de-serializing and, thus, need to be written in output medium. 

Remarks

To serialize object data, usually there are no need to use BeginObject method, the real Delphi object or record can be serialized using a single call to  Value method:

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

However, there are cases, when NG.Serialization.TSerializer.EndObject the data is not physically stored as an object or record. In such cases its possible to serialize object like data manually, using BeginObject/ EndObject low-level methods. After a call to BeginObject, object properties should be serialized one by one; when all properties was serialized a call to NG.Serialization.TSerializer.EndObject must be made.  A property should be serialized making a call to Prop method, to begin property serialization and specify its name, following one of the following calls to specify its value:

BeginObject allows to serialize polymorphic object values; that is, values of different run-time type, and thus, with different property sets, can be written at the same serialization point. To de-serialize such values, de-serializer should know the type of written value. Thus, this type should be saved in output medium in such case. To force AType to be saved in output medium, set AWriteType parameter value to True. Precisely speaking, AWriteType parameter value need to be set to True if both following conditions are True:

  • The value is polymorphic, thus, objects of different types can be potentially written here.
  • And, the run-time type of the value is different from the default type for this serialization point.

The second condition is an optimization, which makes serialization output more readable: thus, if de-serializer expect objects of several types at some serialization point, one of such type (usually most frequently used) can be considered as default; so, writing a type name for such default type can be suppressed, setting AWriteType parameter value to False.

Examples

DelphiCopyCode imageCopy Code
S.BeginObject('Book', False);
S.Prop('Tittle').Value('Delphi XE Handbook');
S.Prop('Author').Value('Marco Cantu');
S.EndObject;