Monday, June 9, 2008

.NET Serialization

What Is Serialization?

Serialization is the process of converting an object to a form that can be readily transported. Generally, this means saving the object's data in a way that makes it easy to recreate the object later, through a process called deserialization. Serialization does not save any of the methods of an object—it is assumed that the object's code will be available during the deserialization process, and that the data unique to the object is all that is necessary to recreate the object. The serialized data can take many formats, but they all have one thing in common: the serialized data is not tied to the originating object or system, and the data contains all the information necessary to recreate an object that's identical to the original.


Different Types of Serialization

The Microsoft .NET Framework provides an almost bewildering variety of ways to serialize an object. This chapter focuses on XML serialization, but before we get into the details, I'd like to briefly examine and compare the various serialization methods offered.

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.

Other Forms of Serialization

A number of other forms of serialization are available in the .NET Framework that are either not intended to be used directly or are specific to a problem domain, which is outside the scope of this book. These other serialization methods include the LosFormatter, which is used to serialize and deserialize page view state in ASP.NET, the CodeDomSerializer, which is used to serialize an object graph representing dynamically generated source code, the binary serialization used in .NET Remoting, and others.

Labels: ,

1 Comments:

At July 18, 2016 at 8:34 PM , Blogger Leslie Lim said...

Man I love this site. It is just so awesome. Whatever you want, all are available here.

www.imarksweb.org

 

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home