Object Serialization and De-Serialization

Introduction

Consider the following code:

<!DOCTYPE html>
<html>
<head>
<title>Vehicle Registration</title>
</head>
<body>
@{
    string strFileName = Server.MapPath("~/App_Data/Vehicle.car");

    string make = "Toyota";
    string model = "Camry";
    int year = 2018;
    string color = "Cadet Blue";

    using(FileStream fsVehicle = new FileStream(strFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
    {
        BinaryWriter brVehicle = new BinaryWriter(fsVehicle);

        brVehicle.Write(make);
        brVehicle.Write(model);
        brVehicle.Write(year);
        brVehicle.Write(color);
    }
}
</body>
</html>

This is an example of saving individual data of primitive types:

Saving the variables in a method

In the same way, we learned to save the individual fields of a class:

Saving the individual parts of an object

Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Vehicle Registration</title>
</head>
<body>
@{
    string strFileName = Server.MapPath("~/App_Data/Vehicle.car");

    string make = string.Empty;
    string model = string.Empty;
    int year = 0;
    string color = string.Empty;

    using(FileStream fsVehicle = new FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
        BinaryReader brVehicle = new BinaryReader(fsVehicle);

        make = brVehicle.ReadString();
        model = @brVehicle.ReadString();
        year = brVehicle.ReadInt32();
        color = brVehicle.ReadString();
    }
}

<p>Make: @make<br />
Model: @model<br />
Year: @year<br />
Color: @color</p>
</body>
</html>

When it comes to a class, the problem with saving individual fields is that you could forget to save one of the parts (fields or properties). Object serialization consists of saving a whole object as one instead of its individual fields:

Serialization

In other words, a variable declared from a class can be saved to a stream and then the saved object can be retrieved later. The .NET Framework supports various types of object serialization.

Serialization

Binary serialization works by processing an object rather than streaming its individual member variables. This means that, to use it, you define an object and initialize it, or "fill" it, with the necessary values and any information you judge necessary. This creates a "state" of the object. It is this state that you prepare to serialize. When you save the object, it is converted into a stream.

To perform binary serialization, there are a few steps you must follow. When creating the class whose objects would be serialized, you must mark it with an attribute named Serializable . Here is an example:

using System;

[Serializable]
public class Car
{
    public string Make;
    public string Model;
    public uint   Year;
    public byte   Color;
}

As one way to support serialization, the .NET Framework provides a class named BinaryFormatter. This class is defined in the System.Runtime.Serialization.Formatters.Binary namespace. The BinaryFormatter class is equipped with two constructors. The default constructor is used to simply create an object. After declaring the variable, to allow you to serialize an object, the BinaryFormatter class is equipped with a method named Serialize. This method is overloaded with two versions. One of the versions of this method uses the following syntax:

public void Serialize(Stream serializationStream, object graph);

The first argument to this method must be an object of a Stream-based class. For example, the first argument can be of type FileStream class).

The second argument must be the object to serialize. This means that, before calling this method, you should have built the object. Here is an example of serializing an object:

<!DOCTYPE html>
<html>
<head>
<title>Vehicle Registration</title>
</head>
<body>
@{
    string strFileName = Server.MapPath("~/App_Data/Vehicle.car");

    Car vehicle = new Car();

    vehicle.Make = "Lexus";
    vehicle.Model = "LS";
    vehicle.Year = 2017;
    vehicle.Color = "Roche Lavender";

    using (FileStream fsVehicle = new FileStream(strFileName, FileMode.Create,
                                                              FileAccess.ReadWrite,
                                                              FileShare.ReadWrite))
    {
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bfVehicle =
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        
        bfVehicle.Serialize(fsVehicle, vehicle);
    }
}
</body>
</html>

De-Serialization

As serialization is the process of storing an object to a medium, the opposite, which is deserialization is used to retrieve an object from a stream. To support this, the BinaryFormatter class is equipped with a method named Deserialize(). Like Serialize(), the Deserialize() method is overloaded with two versions. One of them uses the following syntax:

public object Deserialize(Stream serializationStream);

This method takes as argument a Stream-based object, such as a FileStream variable, that indicates where the file is located. The Deserialize() method returns an Object object. As a goal, you want the Deserialize() method to produce the type of object that was saved so you can retrieve the values that the returned object holds. Because the method returns an Object value, you must cast the returned value to the type of your class.

Once the Deserialize() method has returned the desired object, you can access its values. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Vehicle Registration</title>
</head>
<body>
@{
    string strFileName = Server.MapPath("~/App_Data/Vehicle.car");

    Car vehicle = new Car();

    using (FileStream fsVehicle = new FileStream(strFileName, FileMode.Open,
                                                              FileAccess.Read,
                                                              FileShare.Read))
    {
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bfVehicle =
                new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        vehicle = (Car)bfVehicle.Deserialize(fsVehicle);
    }
}

<p>Make: @vehicle.Make<br />
Model: @vehicle.Model<br />
Year: @vehicle.Year<br />
Color: @vehicle.Color</p>
</body>
</html>

Topics on Serialization

Partial Serialization

In the examples we have used so far, we were saving the whole object. You can make it possible to save only some parts of the class. When creating a class, you can specify what fields or properties would be serialized and which ones would not be. To specify that a member cannot be saved, you can mark it with the [NonSerialized] attribute. Here is an example:

[Serializable]
public class Car
{
    public string Make;
    public string Model;

    // Because the value of a car can change,
    // there is no reason to save it
    [NonSerialized]
    public decimal Value;
    public uint Year;
    public byte Color;
}

After creating the class, you can declare a variable of it and serialize it as we saw already. When you serialize this type of object, the member(s) marked with the the [NonSerialized] attribute would not be saved. After the object has been serialized, you can then retrieve the object and its values, using the technique we learned earlier.

Implementing a Custom Serialized Class

To support serialization, the .NET Framework provides an interface named ISerializable. You can create a class that implements this interface to customize the serialization process. Even if you plan to use this interface, the class you create must be marked with the [Serializable] attribute.

.NET Built-In Serialized Classes

The .NET Framework is filled with many classes for serialization. To know that a class is ready for serialization, when viewing its documentation either in the Docs (or MSDN) web site or in the help documentation, check that it is marked with the [SerializableAttribute]. Here is an example of such as class:

The Serializable attribute of a built-in class

Some of these classes provide the properties and methods to create an object and directly save it. For some other classes, you must first create a class, mark it with the [SerializableAttribute] attribute, build an object of it, and then pass it to the .NET class.


Home Copyright © 2008-2019, FunctionX Next