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:
Delphi | Copy 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:
- Calling Value method.
- Calling NG.Serialization.TSerializer.Null method, if property value is Null.
- Calling NG.Serialization.TSerializer.BeginObject(string,Boolean)/NG.Serialization.TSerializer.EndObject methods, if property value is an object itself.
- Calling NG.Serialization.TSerializer.BeginArray(string)/NG.Serialization.TSerializer.EndArray methods, if property value is an array.
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
Delphi | Copy Code |
---|---|
S.BeginObject('Book', False); S.Prop('Tittle').Value('Delphi XE Handbook'); S.Prop('Author').Value('Marco Cantu'); S.EndObject; |