Click here to Skip to main content
15,881,881 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

What is serialization and deserialization in c#?
Can anybody describe me details and what its benifit to use.

Regards,
Purnananda Behera
Posted

You can define serialization and deserialization in the below manner
Serialize means (convert an object instance to an XML document)

Deserialize means (convert an XML document into an object instance)

Instead of XML you can also go with JSON, binary, text etc.

see this
http://msdn.microsoft.com/en-us/library/ms731073.aspx[^]

Serialization and Deserialization in ASP.NET with C#[^]

http://sharpertutorials.com/serialization/[^]
 
Share this answer
 
v5
Comments
Mehdi Gholam 4-Nov-11 5:42am    
Not necessarily to XML, you can go to JSON, binary, text etc.
P.Salini 4-Nov-11 5:57am    
I missed to specify that
Thanks Mehdi for correction.
Serialization is the process of going from in memory representation of an object to a disk based format which can be in any form like binary, JSON, BSON, XML, etc.

Deserialization is the reverse process, in which you get an object from the disk based format.

This is mainly used in document store, ORM style databases, storing config files etc.

The main benefit is the speed in populating an object as opposed to querying multiple tables in the case of database storage.

Simplicity in coding is another benefit, as you don't need to know tables columns etc.
 
Share this answer
 
'Serialization' is the saving/preservation of the 'structure' and/or 'state' of objects, or data. The target that you 'save' to may be in memory, on a hard-disk, or by internet protocols, or other protocols, to a local or remote database, server, NAS, etc.

'Deserialization' is the reading back in from the same types of storage you 'saved' to listed above to: refresh the state of existing objects by setting their properties, field, etc. Or you may 'deserialize' to completely re-create classes, user interfaces, and code, instances of classes, values for properties, fields, etc.

Both Serialization and Deserialization (S&D) can utilize a wide variety of formats, some of which are built-in to .NET. Classes in .NET can be marked as 'serializable' via the use of 'Attributes' within certain guidelines.

We can speak of low-level (bunch of bytes: i.e., binary) S&D formats, and we can speak of 'high-level' formats which are somewhat human readable such as XML and JSON.

Suggestion: take a look at Mehdi Gholam's current JSON serializer and de-serializer, "fastJSON"[^], here on CodeProject to see a state-of-the-art implementation of use of a 'high-level' format for S&D.
 
Share this answer
 
v3
Serialization is the process of converting the state of an object into a form that can be persisted in a storage medium or transported across the processes/machines. The opposite of serialization is deserialization which is a process that converts the outcome of serialization into the original object.
 
Share this answer
 
Serialization (known as pickling in python) is an easy way to convert an object to a binary representation that can then be e.g. written to disk or sent over a wire.

It's useful e.g. for easy saving of settings to a file.

You can serialize your own classes if you mark them with [Serializable] attribute. This serializes all members of a class, except those marked as [NonSerialized].

.NET offers 2 serializers: binary, SOAP, XML. The difference between binary and SOAP is:

binary is more efficient (time and memory used)
binary is not human-readable. SOAP isn't much better.

XML is slightly different:

it lives in System.Xml.Serialization
it uses [XmlIgnore] instead of [NonSerialized] and ignores [Serializable]
it doesn't serialize private class members

An example of serialization/deserialization to a file:

using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
public class MySettings {
    public int screenDx;
    public ArrayList recentlyOpenedFiles;
    [NonSerialized]public string dummy;
}

public class Settings {
    const int VERSION = 1;
    static void Save(MySettings settings, string fileName) {
            Stream stream = null;
            try {
                IFormatter formatter = new BinaryFormatter();
                stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
                formatter.Serialize(stream, VERSION);
                formatter.Serialize(stream, settings);
            } catch {
                // do nothing, just ignore any possible errors
            } finally {
                if (null != stream)
                    stream.Close();
            }
    }
    
    static MySettings Load(string fileName) {
        Stream stream = null;
        MySettings settings = null;
        try {
            IFormatter formatter = new BinaryFormatter();
            stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
            int version = (int)formatter.Deserialize(stream);
            Debug.Assert(version == VERSION);
            settings = (MySettings)formatter.Deserialize(stream);
        } catch {
            // do nothing, just ignore any possible errors
        } finally {
            if (null != stream)
                stream.Close();
        }
        return settings;
    }
}
 
Share this answer
 
Comments
purnananda behera 4-Nov-11 8:55am    
Thanks dear
Gaurav Joshi 30-May-14 2:54am    
There are many other formats also, besides binary and xml, like JSON which is most widely used with AJAX.
Have a look at Google Search Result on this topic.
Google Search Result
 
Share this answer
 
 
Share this answer
 
v2
serialization is the process of conversion an object so that you can pass it into any stream and vice versa
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900