Serialization with .NET
(Page 1 of 4 )
Initializing and manipulating objects in a program is fairly straightforward, but in some situations, this isn't enough. Rather than just being used and discarded, it's sometimes necessary for an object to be preserved for later use. It's also sometimes necessary for objects to be sent to another computer. This process of preserving objects is known as serialization.
When an object is serialized, its state is stored in such a way that the object can be easily remade at a later time or on a different machine. .NET provides several methods of serializing (and deserializing) objects, as well as several formats in which the object can be saved. This article will explore the serialization functionality provided by .NET.
Simple Serialization
To get started, let's serialize a simple object: a string containing a short message. We'll store the string on disk and then restore it by reading the file. Here's the required code, which we'll examine in detail:
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
class StringSerialization
{
public static void Main(string[] args)
{
// The string we want to serialize
string message = "This is a message.";
Console.WriteLine(message);
// Serialization takes place here
FileStream myStream = File.Create("message");
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(myStream, message);
// Clear our string
message = null;
// Restore the contents of our string -- deserialization
myStream.Position = 0;
message = (string)formatter.Deserialize(myStream);
myStream.Close();
Console.WriteLine(message);
}
}
The above code saves the message in a file named message. It then clears the contents of the message (just for fun) and then restores the saved object.
As you can see, .NET's mechanisms for serialization are located in the System.Runtime.Serialization namespace. Here, we make use of the namespace System.Runtime.Serialization.Formatters.Binary. Formatters contains, as its name implies, different formatters for serialization. The formatter is in charge of exactly how the object to be serialized is represented. Here, we use BinaryFormatter. You can see how BinaryFormatter formats our string by examining the contents of the message file, which look something like this:
����This is a message.
Since we're only serializing a string, the file remains somewhat readable, with the exception of a number of special characters. However, with more complicated objects, the output produced by BinaryFormatter is quite unreadable. In many situations, it doesn't matter what the serialized object looks like; you may not care. However, in other situations, the serialized object may need to be more human-readable. This is a downside to the BinaryFormatter, but, thankfully, this isn't the only way to represent serialized data in .NET.
After message is initialized, a FileStream is created, which provides read/write access:
FileStream myStream = File.Create("message");
Note, however, that any type of Stream will do. Next, we create a BinaryFormatter, whose function was already explained:
BinaryFormatter formatter = new BinaryFormatter();
The object is then serialized simply by calling the Serialize method of our BinaryFormatter object:
formatter.Serialize(myStream, message);
Next, our position in myStream is reset to the beginning of the file so that we can begin reading. The process of deserialization is no less simple than the process of serialization. The Deserialize method is called, and myStream is passed:
message = (string)formatter.Deserialize(myStream);
Deserialize returns the object contained within the Stream, which must be cast to the proper type -- here, it is string. Finally, we clean up by closing myStream.
Not hard, is it?
Next: Serializing Custom Types >>
More .NET Articles
More By Peyton McCullough