Deep Serialization: Binary and SOAP Serialization with a Generic Twist

by Joe Finsterwald

This code sample applies to ASP.NET in the .NET 2.0 framework. Examples are written in C#. Download the code

Contents

  1. The Problem
  2. The Solution
  3. Step 1: Binary Serialization to a Byte Array
  4. Step 2: Binary Serialization to a File
  5. Step 3: SOAP Serialization to a String
  6. Step 4: Putting it all together
  7. Conclusion

The Problem:

You have one of the following problems:

  • You need to persist an object to a storage medium.
  • You are using RPC.
  • You need to stream to a socket.

The solution:

Serialization allows you to convert a graph of objects into a linear sequence of bytes, XML, or SOAP. Once an object is flattened you can store it or send it over the wire. Serialized data can then be deserialized. Deserialization essentially reconstitutes the graph of objects you initially serialized. Serialization sounds complicated, but in practice it’s pretty easy because the .NET framework takes care of most of the complexity for you.

Microsoft's .NET framework provides two different options for persisting data or sending it over the network: shallow and deep serialization. Shallow serialization allows for the serialization of read-write property values of an object. Private data members and objects referenced by the object being serialized are ignored. Shallow serialization is used by Web Services and the XmlSerializer.

As you may have guessed from the title of this article the focus of this code sample is on deep serialization. Deep serialization serializes the whole object and any objects referenced by the object being serialized. This code sample demonstrates how to serialize data using the BinaryFormatter and the SoapFormatter. Both of these classes perform deep serialization.

In order to be eligible for serialization an object must be decorated with the [Serializable] attribute or implement the ISerializable interface. In the event that performance is an issue you can opt to exclude a field in an object from serialization by decorating the field with the [NonSerialized] attribute—this is called selective serialization.

I've written the following code samples to demonstrate how easy it is to implement serialization in your project.

Step 1: Binary Serialization to a Byte Array

Binary serialization uses the BinaryFormatter which is contained in the System.Runtime.Serialization.Formatters.Binary namespace. The BinaryFormatter classes implement the IRemotingFormatter interface to support remote procedure calls (RPCs), and the IFormatter interface (inherited by the IRemotingFormatter) to support serialization of a graph of objects.

Click to View the Code


Step 2: Binary Serialization to a File

An example of binary serialization to a file.

Click to View the Code


Step 3: SOAP Serialization to a String

SOAP serialization uses the SoapFormatter which is contained in the System.Runtime.Serialization.Formatters.Soap namespace. SOAP Serialization is more versatile than binary serialization, but versatility comes at a significant cost in terms of performance.

Add Reference

Click to View the Code


Step 4: Putting it all together

I've put it altogether in one place if you want to include this code in a class library.

Click to View the Code


Conclusion:

Having an understanding of how serialization works is critically important if you want to be effective as a developer. Hopefully this example has demonstrated that deep serialization is both easy to implement and powerful.

Comments

Subject Name Date Submitted
Yes knowing serialization uis very important
Vikram9/14/2006 1:07:27 AM
Serialization
Igor9/14/2006 2:11:35 AM
re: Serialization
Joe9/18/2006 2:55:55 PM
Very useful article
Sebastian9/14/2006 10:30:08 AM
Useful
Ashok Kumar9/16/2006 8:30:03 AM
What is the T in T SoapDeserialization
Aleksey9/25/2006 1:49:34 PM
re: What is the T in T SoapDeserialization
Joe9/25/2006 5:32:45 PM
re: What is the T in T SoapDeserialization
Aleksey9/26/2006 9:37:56 AM
sr in SoapDeserailization method
Aleksey9/26/2006 9:50:04 AM
re: sr in SoapDeserailization method
Joe9/26/2006 1:47:29 PM
New Comment
(Your email address will not be displayed or shared.)
Please enter the code shown below. If you cannot read it, press "reset image" to generate a new one.