Click or drag to resize

SerializationWriter Class

Class which defines the writer for serialized data using the fast serialization optimization. A SerializationWriter instance is used to store values and objects in a byte array.

Once an instance is created, use the various methods to store the required data. ToArray() will return a byte[] containing all of the data required for deserialization. This can be stored in the SerializationInfo parameter in an ISerializable.GetObjectData() method.

As an alternative to ToArray(), if you want to apply some post-processing to the serialized bytes, such as compression, call AppendTokenTables first to ensure that the string and object token tables are appended to the stream, and then cast BaseStream to MemoryStream. You can then access the MemoryStream's internal buffer as follows:

Examples
writer.AppendTokenTables();
MemoryStream stream = (MemoryStream) writer.BaseStream;
serializedData = MyCompressor.Compress(stream.GetBuffer(), (int)stream.Length);
Inheritance Hierarchy
SystemObject
  System.IOBinaryWriter
    SD.LLBLGen.Pro.ORMSupportClassesSerializationWriter

Namespace:  SD.LLBLGen.Pro.ORMSupportClasses
Assembly:  SD.LLBLGen.Pro.ORMSupportClasses (in SD.LLBLGen.Pro.ORMSupportClasses.dll) Version: 5.11.0.0 (5.11.23.1114)
Syntax
public sealed class SerializationWriter : BinaryWriter

The SerializationWriter type exposes the following members.

Constructors
  NameDescription
Public methodSerializationWriter
Creates a FastSerializer with the Default Capacity (1kb)
Public methodSerializationWriter(Int32)
Creates a FastSerializer with the specified capacity
Public methodSerializationWriter(Stream)
Creates a FastSerializer around the specified stream Will store token table presize info if the stream is seekable
Public methodSerializationWriter(Stream, Boolean)
Creates a FastSerializer around the specified stream Notes: If the stream is not seekable then the allowStoreTokenTablePresizeInfo parameter is ignored
Top
Properties
  NameDescription
Public propertyAllowStoreTokenTablePresizeInfo
Gets a boolean flag which indicates whether AppendTokenTables will store the presize information for the string and object token tables.
Public propertyBaseStream
Gets the underlying stream of the BinaryWriter.
(Inherited from BinaryWriter.)
Public propertyObjectTokenTableSize
Returns the number of objects in the object token table.
Public propertyOptimizeForSize
Gets or Sets a boolean flag to indicate whether to optimize for size (default) by storing data as packed bits or sections where possible. Setting this value to false will turn off this optimization and store data directly which increases the speed. Note: This only affects optimization of data passed to the WriteObject method and direct calls to the WriteOptimized methods will always pack data into the smallest space where possible.
Public propertyPreserveDecimalScale
Gets or Sets a boolean flag to indicate whether to preserve the scale within a Decimal value when it would have no effect on the represented value. Note: a 2m value and a 2.00m value represent the same value but internally they are stored differently - the former has a value of 2 and a scale of 0 and the latter has a value of 200 and a scale of 2. The scaling factor also preserves any trailing zeroes in a Decimal number. Trailing zeroes do not affect the value of a Decimal number in arithmetic or comparison operations. However, trailing zeroes can be revealed by the ToString method if an appropriate format string is applied. From a serialization point of view, the former will take 2 bytes whereas the latter would take 4 bytes, therefore it is preferable to not save the scale where it doesn't affect the represented value.
Public propertyStringTokenTableSize
Returns the number of strings in the string token table.
Public propertyStatic memberTypeSurrogates
gets the list of optional IFastSerializationTypeSurrogate instances which SerializationWriter and SerializationReader will use to serialize objects not directly supported. It is important to use the same list on both client and server ends to ensure that the same surrogated-types are supported.
Top
Methods
  NameDescription
Public methodAppendTokenTables
Ensures that the size of the string and object token tables are appended to the stream and that their offset is written into the first 4 bytes of the stream. Does nothing if the stream is not seekable or the constructor specified not to store presize information. Notes: Called automatically by ToArray() otherwise must be called manually.
Public methodClose
Closes the current BinaryWriter and the underlying stream.
(Inherited from BinaryWriter.)
Public methodDispose
Releases all resources used by the current instance of the BinaryWriter class.
(Inherited from BinaryWriter.)
Public methodDumpTypeUsage
Dumps the type usage.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodFlush
Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
(Inherited from BinaryWriter.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodSeek
Sets the position within the current stream.
(Inherited from BinaryWriter.)
Public methodToArray
Returns a byte[] containing all of the serialized data where the underlying stream is a MemoryStream. Only call this method once all of the data has been serialized.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodWrite(Boolean)
Writes a one-byte Boolean value to the current stream, with 0 representing false and 1 representing true.
(Inherited from BinaryWriter.)
Public methodWrite(Byte)
Writes an unsigned byte to the current stream and advances the stream position by one byte.
(Inherited from BinaryWriter.)
Public methodWrite(SByte)
Writes a signed byte to the current stream and advances the stream position by one byte.
(Inherited from BinaryWriter.)
Public methodWrite(Char)
Writes a Unicode character to the current stream and advances the current position of the stream in accordance with the Encoding used and the specific characters being written to the stream.
(Inherited from BinaryWriter.)
Public methodWrite(Double)
Writes an eight-byte floating-point value to the current stream and advances the stream position by eight bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Decimal)
Writes a decimal value to the current stream and advances the stream position by sixteen bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Int16)
Writes a two-byte signed integer to the current stream and advances the stream position by two bytes.
(Inherited from BinaryWriter.)
Public methodWrite(UInt16)
Writes a two-byte unsigned integer to the current stream and advances the stream position by two bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Int32)
Writes a four-byte signed integer to the current stream and advances the stream position by four bytes.
(Inherited from BinaryWriter.)
Public methodWrite(UInt32)
Writes a four-byte unsigned integer to the current stream and advances the stream position by four bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Int64)
Writes an eight-byte signed integer to the current stream and advances the stream position by eight bytes.
(Inherited from BinaryWriter.)
Public methodWrite(UInt64)
Writes an eight-byte unsigned integer to the current stream and advances the stream position by eight bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Single)
Writes a four-byte floating-point value to the current stream and advances the stream position by four bytes.
(Inherited from BinaryWriter.)
Public methodWrite(Boolean)
Writes a Boolean[] into the stream. Notes: A null or empty array will take 1 byte. Calls WriteOptimized(Boolean[]).
Public methodWrite(Byte)
Writes a Byte[] into the stream. Notes: A null or empty array will take 1 byte.
(Overrides BinaryWriterWrite(Byte).)
Public methodWrite(Char)
Writes a Char[] into the stream. Notes: A null or empty array will take 1 byte.
(Overrides BinaryWriterWrite(Char).)
Public methodWrite(ArrayList)
Writes an ArrayList into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on data content Notes: A null Arraylist takes 1 byte. An empty ArrayList takes 2 bytes. The contents are stored using WriteOptimized(ArrayList) which should be used if the ArrayList is guaranteed never to be null.
Public methodWrite(BitArray)
Writes a BitArray value into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on data content Notes: A null BitArray takes 1 byte. An empty BitArray takes 2 bytes.
Public methodWrite(BitVector32)
Writes a BitVector32 into the stream. Stored Size: 4 bytes.
Public methodWrite(DateTime)
Writes a DateTime value into the stream. Stored Size: 8 bytes
Public methodWrite(DateTime)
Writes a DateTime[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Decimal)
Writes a Decimal[] into the stream. Notes: A null or empty array will take 1 byte. Calls WriteOptimized(Decimal[]).
Public methodWrite(Double)
Writes a Double[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Guid)
Writes a Guid into the stream. Stored Size: 16 bytes.
Public methodWrite(Guid)
Writes a Guid[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Int16)
Writes an Int16[]or a null into the stream. Notes: A null or empty array will take 1 byte. Calls WriteOptimized(decimal[]).
Public methodWrite(Int32)
Writes an Int32[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Int64)
Writes an Int64[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Object)
Writes an object[] into the stream. Stored Size: 2 bytes upwards depending on data content Notes: A null object[] takes 1 byte. An empty object[] takes 2 bytes. The contents of the array will be stored optimized.
Public methodWrite(SByte)
Writes an SByte[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Single)
Writes a Single[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(String)
Calls WriteOptimized(string). This override to hide base BinaryWriter.Write(string).
(Overrides BinaryWriterWrite(String).)
Public methodWrite(TimeSpan)
Writes a TimeSpan value into the stream. Stored Size: 8 bytes
Public methodWrite(TimeSpan)
Writes a TimeSpan[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(UInt16)
Writes a UInt16[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(UInt32)
Writes a UInt32[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(UInt64)
Writes a UInt64[] into the stream. Notes: A null or empty array will take 1 byte.
Public methodWrite(Type, Boolean)
Stores a Type object into the stream. Stored Size: Depends on the length of the Type's name and whether the fullyQualified parameter is set. A null Type takes 1 byte.
Public methodWrite(IOwnedDataSerializable, Object)
Allows any object implementing IOwnedDataSerializable to serialize itself into this SerializationWriter. A context may also be used to give the object an indication of what data to store. As an example, using a BitVector32 gives a list of flags and the object can conditionally store data depending on those flags.
Public methodWrite(Byte, Int32, Int32)
Writes a region of a byte array to the current stream.
(Inherited from BinaryWriter.)
Public methodWrite(Char, Int32, Int32)
Writes a section of a character array to the current stream, and advances the current position of the stream in accordance with the Encoding used and perhaps the specific characters being written to the stream.
(Inherited from BinaryWriter.)
Public methodWriteT(ListT)
Writes a non-null generic List into the stream.
Public methodWriteK, V(DictionaryK, V)
Writes a non-null generic Dictionary into the stream.
Public methodWriteBytesDirect
Writes a byte[] directly into the stream. The size of the array is not stored so only use this method when the number of bytes will be known at deserialization time. A null value will throw an exception
Public methodWriteNullable
Writes a Nullable type into the stream. Synonym for WriteObject().
Public methodWriteObject
Stores an object into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on type and/or content. 1 byte: null, DBNull.Value, Boolean 1 to 2 bytes: Int16, UInt16, Byte, SByte, Char, 1 to 4 bytes: Int32, UInt32, Single, BitVector32 1 to 8 bytes: DateTime, TimeSpan, Double, Int64, UInt64 1 or 16 bytes: Guid 1 plus content: string, object[], byte[], char[], BitArray, Type, ArrayList Any other object be stored using a .Net Binary formatter but this should only be allowed as a last resort: Since this is effectively a different serialization session, there is a possibility of the same shared object being serialized twice or, if the object has a reference directly or indirectly back to the parent object, there is a risk of looping which will throw an exception. The type of object is checked with the most common types being checked first. Each 'section' can be reordered to provide optimum speed but the check for null should always be first and the default serialization always last. Once the type is identified, a SerializedType byte is stored in the stream followed by the data for the object (certain types/values may not require storage of data as the SerializedType may imply the value). For certain objects, if the value is within a certain range then optimized storage may be used. If the value doesn't meet the required optimization criteria then the value is stored directly. The checks for optimization may be disabled by setting the OptimizeForSize property to false in which case the value is stored directly. This could result in a slightly larger stream but there will be a speed increate to compensate.
Public methodWriteOptimized(Boolean)
Writes an optimized Boolean[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte. Stored as a BitArray.
Public methodWriteOptimized(ArrayList)
Writes an non-null ArrayList into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on data content Notes: An empty ArrayList takes 1 byte.
Public methodWriteOptimized(BitArray)
Writes a BitArray into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on data content Notes: An empty BitArray takes 1 byte.
Public methodWriteOptimized(BitVector32)
Writes a BitVector32 into the stream using the fewest number of bytes possible. Stored Size: 1 to 4 bytes. (.Net is 4 bytes) 1 to 7 bits takes 1 byte 8 to 14 bits takes 2 bytes 15 to 21 bits takes 3 bytes 22 to 28 bits takes 4 bytes ------------------------------------------------------------------- 29 to 32 bits takes 5 bytes - use Write(BitVector32) method instead Try to order the BitVector32 masks so that the highest bits are least-likely to be set.
Public methodWriteOptimized(DateTime)
Writes a DateTime value into the stream using the fewest number of bytes possible. Stored Size: 3 bytes to 7 bytes (.Net is 8 bytes) Notes: A DateTime containing only a date takes 3 bytes (except a .NET 2.0 Date with a specified DateTimeKind which will take a minimum of 5 bytes - no further optimization for this situation felt necessary since it is unlikely that a DateTimeKind would be specified without hh:mm also) Date plus hh:mm takes 5 bytes. Date plus hh:mm:ss takes 6 bytes. Date plus hh:mm:ss.fff takes 7 bytes.
Public methodWriteOptimized(DateTime)
Writes a DateTime[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Decimal)
Writes a Decimal value into the stream using the fewest number of bytes possible. Stored Size: 1 byte to 14 bytes (.Net is 16 bytes) Restrictions: None
Public methodWriteOptimized(Decimal)
Writes a Decimal[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Int16)
Write an Int16 value using the fewest number of bytes possible.
Public methodWriteOptimized(Int16)
Writes an Int16[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Int32)
Write an Int32 value using the fewest number of bytes possible.
Public methodWriteOptimized(Int32)
Writes an Int32[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Int64)
Write an Int64 value using the fewest number of bytes possible.
Public methodWriteOptimized(Int64)
Writes an Int64[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Object)
Writes a not-null object[] into the stream using the fewest number of bytes possible. Stored Size: 2 bytes upwards depending on data content Notes: An empty object[] takes 1 byte. The contents of the array will be stored optimized.
Public methodWriteOptimized(String)
Writes a string value into the stream using the fewest number of bytes possible. Stored Size: 1 byte upwards depending on string length Notes: Encodes null, Empty, 'Y', 'N', ' ' values as a single byte Any other single char string is stored as two bytes All other strings are stored in a string token list: The TypeCode representing the current string token list is written first (1 byte), followed by the string token itself (1-4 bytes) When the current string list has reached 128 values then a new string list is generated and that is used for generating future string tokens. This continues until the maximum number (128) of string lists is in use, after which the string lists are used in a round-robin fashion. By doing this, more lists are created with fewer items which allows a smaller token size to be used for more strings. The first 16,384 strings will use a 1 byte token. The next 2,097,152 strings will use a 2 byte token. (This should suffice for most uses!) The next 268,435,456 strings will use a 3 byte token. (My, that is a lot!!) The next 34,359,738,368 strings will use a 4 byte token. (only shown for completeness!!!)
Public methodWriteOptimized(TimeSpan)
Writes a TimeSpan value into the stream using the fewest number of bytes possible. Stored Size: 2 bytes to 8 bytes (.Net is 8 bytes) Notes: hh:mm (time) are always stored together and take 2 bytes. If seconds are present then 3 bytes unless (time) is not present in which case 2 bytes since the seconds are stored in the minutes position. If milliseconds are present then 4 bytes. In addition, if days are present they will add 1 to 4 bytes to the above.
Public methodWriteOptimized(TimeSpan)
Writes a TimeSpan[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Type)
Stores a non-null Type object into the stream. Stored Size: Depends on the length of the Type's name. If the type is a System type (mscorlib) then it is stored without assembly name information, otherwise the Type's AssemblyQualifiedName is used.
Public methodWriteOptimized(UInt16)
Write a UInt16 value using the fewest number of bytes possible.
Public methodWriteOptimized(UInt16)
Writes a UInt16[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(UInt32)
Write a UInt32 value using the fewest number of bytes possible.
Public methodWriteOptimized(UInt32)
Writes a UInt32[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(UInt64)
Write a UInt64 value using the fewest number of bytes possible.
Public methodWriteOptimized(UInt64)
Writes a UInt64[] into the stream using the fewest possible bytes. Notes: A null or empty array will take 1 byte.
Public methodWriteOptimized(Object, Object)
Writes a pair of object[] arrays into the stream using the fewest number of bytes possible. The arrays must not be null and must have the same length The first array's values are written optimized The second array's values are compared against the first and, where identical, will be stored using a single byte. Useful for storing entity data where there is a before-change and after-change set of value pairs and, typically, only a few of the values will have changed.
Public methodWriteStringDirect
Writes a non-null string directly to the stream without tokenization.
Public methodWriteTokenizedObject(Object)
Writes a token (an Int32 taking 1 to 4 bytes) into the stream that represents the object instance. The same token will always be used for the same object instance. The object will be serialized once and recreated at deserialization time. Calls to SerializationReader.ReadTokenizedObject() will retrieve the same object instance.
Public methodWriteTokenizedObject(Object, Boolean)
Writes a token (an Int32 taking 1 to 4 bytes) into the stream that represents the object instance. The same token will always be used for the same object instance. When recreateFromType is set to true, the object's Type will be stored and the object recreated using Activator.GetInstance with a parameterless contructor. This is useful for stateless, factory-type classes. When recreateFromType is set to false, the object will be serialized once and recreated at deserialization time. Calls to SerializationReader.ReadTokenizedObject() will retrieve the same object instance.
Public methodWriteTypedArray
Writes a null or a typed array into the stream.
Top
Fields
  NameDescription
Public fieldStatic memberDefaultCapacity
Default capacity for the underlying MemoryStream
Public fieldStatic memberDefaultOptimizeForSize
The Default setting for the OptimizeForSize property.
Public fieldStatic memberDefaultPreserveDecimalScale
The Default setting for the PreserveDecimalScale property.
Public fieldStatic memberHighestOptimizable16BitValue
Holds the highest Int16 that can be optimized into less than the normal 2 bytes
Public fieldStatic memberHighestOptimizable32BitValue
Holds the highest Int32 that can be optimized into less than the normal 4 bytes
Public fieldStatic memberHighestOptimizable64BitValue
Holds the highest Int64 that can be optimized into less than the normal 8 bytes
Top
See Also