Saturday 15 October 2011

Different Types of Serialization

The Microsoft .NET Framework provides an almost bewildering variety of ways to serialize an object.

XML Serialization

XML serialization allows the public properties and fields of an object to be reduced to an XML document that describes the publicly visible state of the object. This method serializes only public properties and fields—private data will not be persisted, so XML serialization does not provide full fidelity with the original object in all cases. However, because the persistence format is XML, the data being saved can be read and manipulated in a variety of ways and on multiple platforms.
The benefits of XML serialization include the following:
  • Allows for complete and flexible control over the format and schema of the XML produced by serialization.
  • Serialized format is both human-readable and machine-readable.
  • Easy to implement. Does not require any custom serialization-related code in the object to be serialized.
  • The XML Schema Definition tool (xsd.exe) can generate an XSD Schema from a set of serializable classes, and generate a set of serializable classes from an XSD Schema, making it easy to programmatically consume and manipulate nearly any XML data in an object-oriented (rather than XML-oriented) fashion.
  • Objects to be serialized do not need to be explicitly configured for serialization, either by the SerializableAttribute or by implementing the ISerializable interface.
The restrictions of XML serialization include the following:
  • The class to be serialized must have a default (parameterless) public constructor.
  • Read-only properties are not persisted.
  • Only public properties and fields can be serialized.

SOAP Serialization

SOAP serialization is similar to XML serialization in that the objects being serialized are persisted as XML. The similarity, however, ends there. The classes used for SOAP serialization reside in the System.Runtime.Serialization namespace rather than the System.Xml.Serialization namespace used by XML serialization. The run-time serialization classes (which include both the SoapFormatter and the BinaryFormatter classes) use a completely different mechanism for serialization than the XmlSerializer class.
The benefits of SOAP serialization include the following:
  • Produces a fully SOAP-compliant envelope that can be processed by any system or service that understands SOAP.
  • Supports either objects that implement the ISerializable interface to control their own serialization, or objects that are marked with the SerializableAttribute attribute.
  • Can deserialize a SOAP envelope into a compatible set of objects.
  • Can serialize and restore non-public and public members of an object.
The restrictions of SOAP serialization include the following:
  • The class to be serialized must either be marked with the SerializableAttribute attribute, or must implement the ISerializable interface and control its own serialization and deserialization.
  • Only understands SOAP. It cannot work with arbitrary XML schemas.

Binary Serialization

Binary serialization allows the serialization of an object into a binary stream, and restoration from a binary stream into an object. This method can be faster than XML serialization, and the binary representation is usually much more compact than an XML representation. However, this performance comes at the cost of cross-platform compatibility and human readability.
The benefits of binary serialization include the following:
  • It's the fastest serialization method because it does not have the overhead of generating an XML document during the serialization process.
  • The resulting binary data is more compact than an XML string, so it takes up less storage space and can be transmitted quickly.
  • Supports either objects that implement the ISerializable interface to control its own serialization, or objects that are marked with the SerializableAttribute attribute.
  • Can serialize and restore non-public and public members of an object.
The restrictions of binary serialization include the following:
  • The class to be serialized must either be marked with the SerializableAttribute attribute, or must implement the ISerializable interface and control its own serialization and deserialization.
  • The binary format produced is specific to the .NET Framework and it cannot be easily used from other systems or platforms.
  • The binary format is not human-readable, which makes it more difficult to work with if the original program that produced the data is not available.

No comments:

Post a Comment