What is serialization?

Posted by Bubbly under ASP.NET on 2/2/2010 | Views : 2016 | Status : [Member] | Replies : 8
What is serialization? Need to use it. Where is it being used? Advantages & Disadvantages

++
Thanks & Regards,
Deepika



Responses

Posted by: Sagarp on: 2/2/2010 [Member] Bronze

Up
0
Down
serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file, a memory buffer, or transmitted across a network connection link to be "resurrected" later in the same or another computer environment.

uses: it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.

The advantage of serialzation is the ability to transmit data across a
network in a cross-platform-compatible format, as well as saving it in a
storage medium in a non-proprietary format.

#The chief disadvantage is the overhead involved in serializing and
de-serializing data, as well as latency issues with transmitting text over a
TCP network.
# The Serializable interface does not offer fine-grained control over object access - although you can somewhat circumvent this issue by implementing the complex Externalizable interface, instead.

# Since serialization does not offer any transaction control mechanisms per se, it is not suitable for use within applications needing concurrent access without making use of additional APIs.

ref this link: http://social.msdn.microsoft.com/Search/en-us/?Query=serialization

Thanks
SagarP
http://www.emanonsolutions.net
http://emanonsolutions.blogspot.com/

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/2/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
In one word, If you want to store / send your own application object over some medium, say you want to store an object of your class to a File, you use Serialization.

Generally ValueTypes are Serialized automatically, for Complex Types, you need to write code on how you want to serialize.

www.abhisheksur.com

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Pauldaniel on: 2/2/2010 [Member] Starter

Up
0
Down
Refer this link
http://www.15seconds.com/issue/020903.htm

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubbly on: 2/3/2010 [Member] Bronze

Up
0
Down
Thanks for providing me the information. Would like to know more points about it.

++
Thanks & Regards,
Deepika

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/3/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Just let me give you an example.

Say you have a class, and you want to serialize.


[Serializable]

public class SerializeClass {
public int intval = 0;
public String str = null;
public double doublevalue = 0;
}

SerializeClass obj = new SerializeClass();
obj.intval = 1;
obj.str = "String Value";
obj.doublevalue = 20.4;

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("serialized.dat", FileMode.Create,FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();


Now if you define
[NonSerialized]public String str = null;

Then you will not get it serialized.

To Deserialize, you use
SerializeClass obj = (SerializeClass ) formatter.Deserialize(objStream);

There are lots of Formatter available like :
SoapFormatter, TextFormatter etc

:)

www.abhisheksur.com

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubbly on: 2/4/2010 [Member] Bronze

Up
0
Down
Thanks for the information Abhi!!

Any more points??

++
Thanks & Regards,
Deepika

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/4/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
This is actually the start.

Structures are itself serializable. Using Serializable attributes is one way to serialize a class. In this case you have to mention what to serialize and what not using Serializable and NonSerialized attributes.

For complex types you can also inherit the class from ISerializable interface and use its GetObjectData to define what to Serialize for your class. See the example :

public class SerializeClass : ISerializable

{

public int intval = 0;
public String str = null;
public double doublevalue = 0;
public SerializeClass()
{
}
void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)
{
oInfo.AddValue("intval ", this.intval);
oInfo.AddValue("str ", this.str);
}
}


Thus you can see I am serializing the same thing but using different tech.

Remember, to Serialize, a complex class, it needs to define a blank constructor for every class, as it try to read through reflection while you use formatter to serialize your object.

I think this is all you need.

And I think, the conversation looks like an article. ha ha ..

Cheers.
Abhishek

www.abhisheksur.com

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bubbly on: 2/26/2010 [Member] Bronze

Up
0
Down
Thanks for that explanation Abhishek

++
Thanks & Regards,
Deepika

Bubbly, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response