Click here to Skip to main content
Click here to Skip to main content
Go to top

Serialization Primer - MC++

, 25 May 2002
Rate this:
Please Sign up or sign in to vote.
A basic introduction to serialization using Managed C++

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(); 
        //This single call will serialize the object
        //to a byte stream
        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); 
        //This single call will deserialize the byte
        //stream back to a copy of the original object
        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;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Nish Sivakumar

United States United States
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.

Comments and Discussions

 
QuestionSerializing class with unmanaged base class PinmemberEricFowler8-Mar-07 13:46 
GeneralSerialization Issues PinmemberVsunkara2-Aug-06 5:37 
GeneralHello PinmemberRaj_Sen28-Feb-06 12:40 
GeneralWoohoo, oh wait.... PinmemberMerlinblack9-Mar-05 13:38 
QuestionI imagine it's as easy in C# and VB.NET? PinmemberJörgen Sigvardsson26-Sep-02 11:45 
GeneralNice Article PinmemberKannan Kalyanaraman27-May-02 9:05 
GeneralRe: Nice Article PinmemberNish - Native CPian27-May-02 14:46 
GeneralPerfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 0:51 
GeneralRe: Perfect editing and formatting ;-) PinmemberShog926-May-02 8:23 
GeneralRe: Perfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 13:20 
Shog9 wrote:
*cough* *cough* no size next to downloads *cough* *cough*
 
LOL
 
No wonder that I thought there was something funny looking about my download links!
 
Nish
 

Regards,
Nish
Native CPian.
Born and brought up on CP.
With the CP blood in him.

GeneralRe: Perfect editing and formatting ;-) PinmemberShog926-May-02 14:22 
GeneralRe: Perfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 14:32 
GeneralRe: Perfect editing and formatting ;-) PinmemberPJ Arends26-May-02 14:51 
GeneralRe: Perfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 14:52 
GeneralRe: Perfect editing and formatting ;-) PinmemberMatt Newman26-May-02 15:28 
GeneralRe: Perfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 15:33 
GeneralRe: Perfect editing and formatting ;-) PinmemberMatt Newman26-May-02 16:25 
GeneralRe: Perfect editing and formatting ;-) PinmemberNish - Native CPian26-May-02 17:14 
General*ouch*! PinmemberShog926-May-02 16:38 
GeneralRe: *ouch*! PinmemberNish - Native CPian26-May-02 17:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 26 May 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid