Turn Left - A weblog by InetSolution

.NET Serialization - What is it and how to decide which method to use?

By Justin Gattuso
Posted on Aug 1, 2008

Serialization is the process of converting objects to a stream of information that can then be saved to disk, sent over the network, committed to a database or otherwise stored in a state where it can later be loaded and recreated.

.NET serialization can be accomplished in two ways: (1) automatic serialization, or (2) custom serialization.

Automatic Serialization

Automatic serialization is easy to implement, it's really just a matter of adding meta information tags to the class and member information that should support serialization. This takes advantage of .NET reflection to infer information about an object from it's meta information. This is how something as normally complex as serialization is able to become a mostly automated process capable of saving object state information to disk and loading it back up. While it is easy and relatively quick to implement it has it's drawbacks.

Probably the two biggest issues with it is that it's probably not a very reasonable solution for saving complex object information. Obviously as the size of an object increases, so too does it's saved information size and the speed with which it is saved and loaded. Furthermore, as the developer you don't have as much control over how something gets serialized, you at the whim of the automatic serialization method. You do however, have control over what information gets serialized, for example some member information you might not want to save, you can specify that it not be saved by place a [NonSerialized] attribute before any members that you don't want to be serialized. This can make automatic serialization more of a possibility with more complex objects.

[Serializable]
public class Car
{
private string sName = string.Empty;
private string sType = string.Empty;
private int iYearBuilt = 1908;

[NonSerialized]
private bool bRunning = false;

public Car()
{
}

public void Start()
{
//...
}
}


Custom Serialization

Inevitably there will be situations where the ability to define the serialization process yourself becomes a requirement. The most obvious of situations is when you might have sensitive information that you need to save but need to encrypt it first, automatic serialization will not know what is sensitive or how to secure it so you must take advantage of custom serialization. This route is a little more work to setup (although it's not terribly difficult) but the end result is you have complete control over the serialization and deserialization processes. With this technique you explicitly define what information and how that information is to be serialized through the use the SerializationInfo object that essentially lets you add key/value information to the serialization stream. There are an infinite nummber of ways to serialize the information, if you're using custom serialization, it's expected you already have some idea how you want to save your information and unless you really know what your after, it may be best to use automatic serialization.

class Car : ISerializable
{
    private string sName = string.Empty;
    private string sType = string.Empty;
    private bool bRunning = false;

public void GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
{
oInfo.AddValue("Name", sName);
oInfo.AddValue("Type", sType);

if(bRunning)
{
oInfo.AddValue("Running", "Yes");
}
}
}


The key point to remember about serializing and deserializing information is that you must deserialize your data in the opposite sequence that you serialized your data, which should make sense otherwise you won't load the information the same way and everything will be corrupt.

.NET serialization is an incredibly powerful system that makes the often laborious chore of saving object state information to disk, a lot more manageable, especially considering the near autonomous nature of automated serialization. Through a solid understanding and use of serialization the developer can focus more on the task at hand and spend less time writing routines to save and load information.

Lastly, it's worth noting that by default as of .NET 2.0 the serialization system supports the ability to serialize data to XML, binary and SOAP formats, this is achieved through the use of serialization formatters which can be studied in more depth within MSDN, you are also free to create and develop your own custom formatters. Some have cropped up on the Internet to create faster, smaller and compressed formatters.

Who is InetSolution?

Donovan - Creative Director
Justin - Lead Architect & Developer
Somer - Graphic Designer
Mac - Programmer
Larry - Programmer
Mosh - Programmer
Paul - Technical Sales Architect
Jay - Weekend & Holidays Sys Admin
Karen - Business Development & Client Care
Jason - Project Director

Our Services

Web Design/Development
We practice a user-centered development philosophy. We work with clients who place their customer's needs first. We need to know who will use your site and why.
Secure File Exchange
Turn your website into a state-of-the-art file exchange system, requiring only a web browser, username and a password.
eCommerce Web Development
We have experience, know-how and superior customer support to ensure that your store is profitable and that your investment with InetSolution earns a high return.
Disaster Recovery Hosting
We provide fully-managed SQL server database hosting for companies seeking a warm disaster recovery site.

Category Archives

About InetSolution

We make business websites profitable. We do it with usable design, solid programming and unique, methodical marketing.