Introduction
Serialization means taking objects and converting them into a form by which
they can be stored on disk or transferred across the network. The advantage here
is that objects can persist their state. Which means you can actually save an
object to disk, this process is called serialization, and later you can read the
data back to get the same object, this process is called deserialization.
Serialization is used quite extensively in .NET remoting. The actual process of
serialization basically involves converting all the members of the class along
with the required meta data into bytes which are then dumped to disk or to
a socket. Later during deserialization, these bytes are converted back to the
original object. The bytes are in the form of a stream, meaning they can be read
and written as a stream of bytes. They don't necessarily need to arrive
all at once.
First Program
In this first program we'll see how to make a serializable class. I have a
class called CData. I have marked the class as serializable by marking it with
the [Serializable] attribute. The serialized data has to be encoded in some
particular format. Thus we also need a class to specify what kind of formatting
will be used to encode the byte stream. Here I have used the BinaryFormatter class which uses binary format as the name implies. I have used
the Serialize member function to serialize the object to a stream and I
have used the Deserialize member function to deserialize the stream back to the
object.

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::IO;
[Serializable]
__gc class CData
{
private:
String* m_name;
Int32 m_age;
public:
__property String* get_Name()
{
return m_name;
}
__property void set_Name(String* s)
{
m_name = s;
}
__property Int32 get_Age()
{
return m_age;
}
__property void set_Age(Int32 i)
{
m_age = i;
}
CData()
{
m_name="Default";
m_age=0;
}
};
int wmain(int argc, char **argv)
{
if(argc==1)
{
Console::WriteLine("Usage:- Serialize00 [L/S]");
return 0;
}
if(argv[1][0]=='S')
{
Console::WriteLine("Saving to file");
CData *data = new CData();
data->Name = "Johnny Bravo";
data->Age = 24;
FileStream *fs = new FileStream("data.txt" ,
FileMode::Create, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
bf->Serialize(fs,data);
fs->Close();
}
else if(argv[1][0]=='L')
{
Console::WriteLine("Loading from file");
CData *data;
FileStream *fs = new FileStream("data.txt" ,
FileMode::Open, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
data = (CData*) bf->Deserialize(fs);
Console::WriteLine("data->Name is {0}",
data->Name);
Console::WriteLine("data->Age is {0}",
data->Age.ToString());
fs->Close();
}
else
{
Console::WriteLine("Unknown option");
}
return 0;
}
Second Program
Sometimes we might not want to persist all members of an object. In such
situations we can actually use the [NonSerialized] attribute to mark certain
members as non-serialized. This means they won't be persisted. But I intend to
show you how to achieve this in a different way by implementing the ISerializable interface which gives us a lot more control in the way our object
is serialized and deserialized. Well, we don't do anything special except that
we derive our class from ISerializable . ISerializable has one member function
GetObjectData which we will have to implement. This method is used for
data serialization. We also need to implement an overload of the constructor to
handle deserialization.
void GetObjectData(SerializationInfo *si, StreamingContext sc)
{
si->AddValue("m_name",m_name);
si->AddValue("m_age",m_age);
}
As you can see, we have a SerializationInfo class which holds the data
required to serialize the object. We use the AddValue method to add two named
values. We leave out the member which we don't need to serialize. This is a
rather simple usage of this technique. We could do further customization here if
we wanted to.
CData(SerializationInfo *si, StreamingContext sc)
{
m_name = si->GetString("m_name");
m_age = si->GetInt32("m_age");
}
And here we have the special constructor that is used during deserialization.
As you can see, we use the various GetXXXX functions to retrieve our data
back. It might puzzle you why this was done this way instead of providing us
with a DeSerializeData function. But Microsoft probably have their own funny
whims. One disadvantage is that you won't get any compiler errors even if you
forget to write this special constructor. But an exception will get thrown
during runtime. So the bug won't remain hidden for too long.

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::IO;
[Serializable]
__gc class CData : public ISerializable
{
private:
String* m_name;
Int32 m_age;
String* m_dontsave;
public:
__property String* get_Name()
{
return m_name;
}
__property void set_Name(String* s)
{
m_name = s;
}
__property Int32 get_Age()
{
return m_age;
}
__property void set_Age(Int32 i)
{
m_age = i;
}
__property String* get_DontSave()
{
return m_dontsave;
}
__property void set_DontSave(String* s)
{
m_dontsave = s;
}
void GetObjectData(SerializationInfo *si, StreamingContext sc)
{
si->AddValue("m_name",m_name);
si->AddValue("m_age",m_age);
}
CData()
{
m_name="Default";
m_dontsave = "Default";
}
protected:
CData(SerializationInfo *si, StreamingContext sc)
{
m_name = si->GetString("m_name");
m_age = si->GetInt32("m_age");
}
};
int wmain(int argc, char **argv)
{
if(argc==1)
{
Console::WriteLine("Usage:- Serialize01 [L/S]");
return 0;
}
if(argv[1][0]=='S')
{
Console::WriteLine("Saving to file");
CData *data = new CData();
data->Name = "Johnny Bravo";
data->Age = 24;
data->DontSave = "Hello World";
FileStream *fs = new FileStream("data.txt" ,
FileMode::Create, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
bf->Serialize(fs,data);
fs->Close();
}
else if(argv[1][0]=='L')
{
Console::WriteLine("Loading from file");
CData *data;
FileStream *fs = new FileStream("data.txt" ,
FileMode::Open, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
data = (CData*) bf->Deserialize(fs);
Console::WriteLine("data->Name is {0}",
data->Name);
Console::WriteLine("data->Age is {0}",
data->Age.ToString());
Console::WriteLine("data->DontSave is {0}",
data->DontSave);
fs->Close();
}
else
{
Console::WriteLine("Unknown option");
}
return 0;
}
Third Program
When you derive a class from your serializable class, you must make sure to
mark the derived class as [Serializable] too. Else you'll get a compiler error.
You must also implement both variations of the constructors, as well as
implement GetObjectData(). In GetObjectData you must remember to call the base
class function first.
void GetObjectData(SerializationInfo *si, StreamingContext sc)
{
CData::GetObjectData(si,sc);
si->AddValue("m_new",m_new);
}
Each of the constructors must also call their corresponding base class
constructors. Otherwise there will be problems during deserialization.
CData2() : CData()
{
m_new = "Default";
}
CData2(SerializationInfo *si, StreamingContext sc) : CData(si,sc)
{
m_new = si->GetString("m_new");
}

#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::IO;
[Serializable]
__gc class CData : public ISerializable
{
private:
String* m_name;
Int32 m_age;
String* m_dontsave;
public:
__property String* get_Name()
{
return m_name;
}
__property void set_Name(String* s)
{
m_name = s;
}
__property Int32 get_Age()
{
return m_age;
}
__property void set_Age(Int32 i)
{
m_age = i;
}
__property String* get_DontSave()
{
return m_dontsave;
}
__property void set_DontSave(String* s)
{
m_dontsave = s;
}
void GetObjectData(SerializationInfo *si, StreamingContext sc)
{
si->AddValue("m_name",m_name);
si->AddValue("m_age",m_age);
}
CData()
{
m_name="Default";
m_dontsave = "Default";
}
protected:
CData(SerializationInfo *si, StreamingContext sc)
{
m_name = si->GetString("m_name");
m_age = si->GetInt32("m_age");
}
};
[Serializable]
__gc class CData2 : public CData
{
private:
String *m_new;
public:
__property String* get_NewName()
{
return m_new;
}
__property void set_NewName(String* s)
{
m_new = s;
}
CData2() : CData()
{
m_new = "Default";
}
void GetObjectData(SerializationInfo *si, StreamingContext sc)
{
CData::GetObjectData(si,sc);
si->AddValue("m_new",m_new);
}
protected:
CData2(SerializationInfo *si, StreamingContext sc) : CData(si,sc)
{
m_new = si->GetString("m_new");
}
};
int wmain(int argc, char **argv)
{
if(argc==1)
{
Console::WriteLine("Usage:- Serialize02 [L/S]");
return 0;
}
if(argv[1][0]=='S')
{
Console::WriteLine("Saving to file");
CData2 *data = new CData2();
data->Name = "Johnny Bravo";
data->Age = 24;
data->DontSave = "Hello World";
data->NewName = "Nish";
FileStream *fs = new FileStream("data.txt" ,
FileMode::Create, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
bf->Serialize(fs,data);
fs->Close();
}
else if(argv[1][0]=='L')
{
Console::WriteLine("Loading from file");
CData2 *data;
FileStream *fs = new FileStream("data.txt" ,
FileMode::Open, FileAccess::ReadWrite);
BinaryFormatter *bf = new BinaryFormatter();
data = (CData2*) bf->Deserialize(fs);
Console::WriteLine("data->Name is {0}",
data->Name);
Console::WriteLine("data->Age is {0}",
data->Age.ToString());
Console::WriteLine("data->DontSave is {0}",
data->DontSave);
Console::WriteLine("data->NewName is {0}",
data->NewName);
fs->Close();
}
else
{
Console::WriteLine("Unknown option");
}
return 0;
}
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site -
www.voidnish.com where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
blog.voidnish.com.
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy
Summer Love and Some more Cricket as well as a programming book –
Extending MFC applications with the .NET Framework.
Nish's latest book
C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.