Home

Details 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 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 ref class CCar
{
public:
    String ^Make;
    String ^Model;

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

After creating the class, you can declare a variable of it and serialize it, using either the binary or the SOAP approach. Here is an example:

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;

[Serializable]
public ref class CCar
{
public:
    String ^Make;
    String ^Model;

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

int main()
{
        CCar vehicle = gcnew CCar;

        vehicle->Make = L"Lexus";
        vehicle->Model = L"LS";
        vehicle->Year = 2007;
        vehicle->Color = 4;
        vehicle->Value = 28640M;

        FileStream ^ stmCar = gcnew FileStream(L"LexusLS.car",
                                             FileMode::Create);
        BinaryFormatter ^ bfmCar = gcnew BinaryFormatter;

        bfmCar->Serialize(stmCar, vehicle);


        return 0;
    }
}

You can then retrieve the object and its values, using any of the techniques we learned earlier. Here is an example:

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;

[Serializable]
public ref class CCar
{
public:
    String ^Make;
    String ^Model;

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

int main()
{
    FileStream ^ stmCar = gcnew FileStream(L"LexusLS.car",
                                             FileMode::Open);
    BinaryFormatter bfmCar = new BinaryFormatter();
    CCar ^ vehicle = dynamic_cast<CCar ^>(bfmCar->Deserialize(stmCar));

    Console::WriteLine(L"Car Information");
    Console::WriteLine(L"Make:  {0}", vehicle->Make);
    Console::WriteLine(L"Model: {0}", vehicle->Model);
    Console::WriteLine(L"Year:  {0}", vehicle->Year);
    Console::Write(L"Color: ");
    switch (vehicle->Color)
    {
    case 1:
        Console::WriteLine(L"Black");
        break;
    case 2:
        Console::WriteLine(L"Gray");
        break;
    case 3:
        Console::WriteLine(L"White");
        break;
    case 4:
        Console::WriteLine(L"Red");
        break;
    case 5:
        Console::WriteLine(L"Blue");
        break;
    }
    Console::WriteLine(L"Value:  {0}\n", vehicle->Value);

    return 0;
}

This would produce:

Car Information
Make: Lexus
Model: LS
Year: 2007
Color: Red
Value:  0

Press any key to continue . . .

Notice that the value of the Value field was not saved: it holds the default value of its data type. This indicates that, you can assign a value a [NonSerialized] field and save the object. The value of that field would not be saved but the compiler would not throw an exception.

Implementing a Custom Serialized Class

To support serialization, the .NET Framework provides the ISerializable interface. 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 ready for serialization. To know that a class is ready for serialization, when viewing its documentation either in the 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 [Serializable] attribute, build an object of it, and then pass it to the .NET class.

 

Previous Copyright © 2007-2012 FunctionX Home