Summary

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

Syntax

procedure BeginObject(out AType: string); overload;

Parameters

AType

Type: string

Object type name, extracted from de-serializing medium. Read Remarks section for more details.

Remarks

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

DelphiCopyCode imageCopy Code
obj := D.Value<TMyObject>;

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

BeginObject allows to de-serialize polymorphic object values; that is, values of different run-time type, and thus, with different property sets. 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. Refer to TSerializer.BeginObject method description to learn, when object type is written in output medium.

If object type has been written into output medium, it will be extracted into AType output parameter. And so, de-serialization algorithms will be able to instantiate an object of required type. Otherwise AType parameter will be set to empty string.

Examples

DelphiCopyCode imageCopy Code
D.BeginObject(objtp);

if (objtp <> 'Book') and (objtp <> '') then
  raise Exception.Create('Unexpected object type');
book := TBook.Create;

while D.HasNext do
begin
  if D.Prop = 'Tittle' then
    book.Tittle := D.Value<string>
  else if D.Prop = 'Author' then
    book.Author := D.Value<string>
  else
    raise Exception.Create('Unexpected property name');
end;

S.EndObject;