Serialization Formatters

It’s up to the client to decide which stream type to use for serialization and in which format the data should be represented. A formatter is an object that implements the IFormatter interface, defined in the System.Runtime.Serialization namespace:

    public interface IFormatter
    {
       object Deserialize(Stream serializationStream);
       void Serialize(Stream serializationStream, object graph);
       /* Other methods */
    }

IFormatter’s significant methods are Serialize() and Deserialize(), which perform the actual serialization and deserialization. .NET ships with two formatters: a binary formatter and a Simple Object Access Protocol (SOAP) formatter. The binary formatter generates a compact binary representation of the object’s state. It’s relatively fast to serialize an object into binary format and to deserialize an object from binary format. The SOAP formatter, as the name implies, persists the object’s state using a text-based XML format. Naturally, the SOAP formatter is considerably slower than the binary formatter, because serialization requires composing a SOAP envelope and deserialization requires SOAP parsing. In addition, the resulting serialization data (i.e., the file size or memory footprint) is bigger. The only advantage of using the SOAP formatter is that it’s platform-neutral: you can provide the serialization information (via network stream or file access) to applications running on non-Windows-based platforms, and they can deserialize equivalent objects on ...

Get Programming .NET Components, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.