Summary
TBinarySerializer is a specialized TSerializer class type for writing values in a binary form into standard VCL TStream object.
Syntax
TBinarySerializer = class(TSerializer)
Properties
Methods
Name | Description | |
DoBeginArray | ||
DoBeginObject | ||
DoEndArray | ||
DoEndObject | ||
DoWrite | ||
FlushBuffer | FlushBuffer method allows to flush serializer internal buffer and synchronize resulting stream content. | |
WriteEndMark | WriteEndMark method writes additional stopping mark into resulting stream to allow corresponding de-serializer to be able to correctly stop at this position, even if the stream contains some additional data. |
Remarks
TBinarySerializer object should be created providing a reference to existing TStream object into which it will write serializing data. Implemented in the current version data format are optimized by the speed, not resulting stream size. At the same time its important to understand that the resulting stream will still contain string tags, such as property names and object type names, because resulting data stream should be de-serializable by common engine interface methods.
For improving performance, TBinarySerializer use its own internal buffer; thus real writing to TStream is delayed. Buffer flush is performed automatically, when the serializer object is destroyed, but if TStream content is needed to be valid before serializer destruction, its required to call NG.Serialization.Binary.TBinarySerializer.FlushBuffer method to synchronize stream content.
If the resulting stream is intended to contain more data, then written by TBinarySerializer, that is, if some other data will be written into the stream after serializer data, its required to call NG.Serialization.Binary.TBinarySerializer.WriteEndMark method to store additional stoping mark into the stream. This will allow de-serializer to stop at the serialized data end without attempting to read additional data (which will result in read error or data corruption with high probability). At the same time if the stream will be filled by a single serializer object only, then there are no need to call NG.Serialization.Binary.TBinarySerializer.WriteEndMark method.
Examples
Delphi | Copy Code |
---|---|
stream := TFileStream.Create(...); binszr := TBinarySerializer.Create(stream); try binszr.Value<TMyObject>(obj); finally binszr.Free; stream.Free; end; |