Advertisment

Customizing Serialization in .Net

author-image
PCQ Bureau
New Update

When the serialization/deserialization process needs to be customized, the class requiring the custom implementation must implement the ISerializable interface, which has the following definition:

Advertisment

public interface Iserializable



{


public virtual void GetObjectData(SerializationInfo SInfo, StreamingContext SContext);


}; 

The GetObjectData method is called by the formatter when the serialization process is undergone, when the object members are about to be persisted. This method gives the class implementing custom serialization to work upon the values of the members, for example, to encrypt them, before they are persisted to the storage stream. The SerializationInfo class method, AddValue, is used to add name/value pairs that are persisted by the formatter, as shown below:

public class CustomSerialization : ISerializable



{


// Public members to be serialized..


public string _strName=null;


// implement the GetObjectData method of the ISerializable interface.


// This is invoked by the formatter, when it is writing the data


// to the storage stream.


public void GetObjectData(SerializationInfo SInfo, StreamingContext SContext)


{


// perform custom serialization, by storing the data in the BASE64 format


SInfo.AddValue(“_strName”,Convert.ToBase64String(_strName)); 


}


// Now, when the object will be deserialized, the formatter will reconstruct the


// object, for which it will invoke this constructor.


private CustomSerialization(SerializationInfo SInfo, StreamContext SContext)


{


// set the values of the member variables..


_strName=Convert.FromBase64String(SInfo.GetString(“_strName”));


}


}; 

















Advertisment

As exemplified above, the first parameter to the AddValue method is the name of the member whose value will be persisted, and the second parameter is the value to be persisted. In the above example, the value is converted to a BASE64 string before letting the formatter persist it. Now, when the deserialization process takes place, the formatter has to create a new object. For this, a special constructor has to be implemenetd that takes the SerializationInfo and StreamingContext objects as its parameters. It is in the implementation of this constructor, that the value of the persisted class member is retrieved using the GetString method that returns the value as a string. Since we persisted the value as a BASE64 string, we convert it back from BASE64 and then assign the member its value. There are variations of the GetXXX methods for retrieving the value for different datatypes, like boolean, float, etc. 

An important point to be kept in mind is that the special constructor should be marked as private so that it isn’t publicly accessible, as an object instantiation using this form would be meaningless unless used by the formatter to deserialize the object. 

So, now we have discussed what .Net serialization is all about, how it is to be used, and how it can be customized. Having taken an inside look at this useful means of object state-management, we can utilize it to make our classes more distributable (over WebServices, for example), and can make them hibernate as the need be, for efficient memory utilization, while securing the contained data.

Kumar Gaurav Khanna

Advertisment