.Net C# Development

Serialization and Deserialization in C#

What is Serialization in C#?

 Serialization in C# is the process of bringing an object into a form that it can be written on stream. It’s the process of converting the object into a form so that it can be stored on a file, database, or memory; or, it can be transferred across the network. Its main purpose is to save the state of the object so that it can be recreated when needed. 

serialization

What is Deserialization in C#?

 As the name suggests, deserialization in C# is the reverse process of serialization. It is the process of getting back the serialized object so that it can be loaded into memory. It resurrects the state of the object by setting properties, fields etc. 

Types

  • Binary Serialization
  • XML Serialization
  • JSON Serialization 

Example Here I will be giving you an example of how to serialize and deserialize an object using binary formatter or xml formatter.

Binary:

[Serializable]
         /*used to make the class serializable. If you don't mention this attribute, 
          you will get an error when you try to serialize the class.
          if you dont want to serialise something use [NonSerialized]
         */
        public class person
        {
            public string name { get; set; }
            public int age { get; set; }
        }
        static void Main(string[] args)
        {
            person personobj = new person() { name = "sumit", age = 25 };
            IFormatter formattable = new BinaryFormatter();
            Stream stream = new FileStream("D:\\abc.txt", FileMode.Create, FileAccess.Write);
            formattable.Serialize(stream, personobj);
            stream.Close();
            Console.WriteLine("object serialized");
            Console.WriteLine("Starting Deserialization...");
            stream = new FileStream("D:\\abc.txt", FileMode.Open, FileAccess.Read);
            person personobj1 = (person)formattable.Deserialize(stream);
            Console.WriteLine("Object deserialized.");
            Console.WriteLine("person's name is {0} with age {1}",personobj1.name,personobj1.age);
        }

XML:

public class person
        {
            public string name { get; set; }
            public int age { get; set; }
        }
        public static void Main(string[] args)
        {
            person personobj = new person() { name = "sumit", age = 25 };
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(person));
            using (TextWriter text = new StreamWriter("D:\\abc.xml")){
                xmlSerializer.Serialize(text, personobj);
            }
            Console.WriteLine("object serialized");
            Console.WriteLine("Starting Deserialization...");
            using (TextReader text = new StreamReader("D:\\abc.xml"))
            {
                person personobj1 = (person)xmlSerializer.Deserialize(text);
                Console.WriteLine("Object deserialized.");
                Console.WriteLine("person's name is {0} with age {1}", personobj1.name, personobj1.age);
            }            
        }

JSON:

public class person
        {
            public string name { get; set; }
            public int age { get; set; }
        }
        public static void Main(string[] args)
        {
            person personobj = new person() { name = "sumit", age = 25 };
            string jsontext = JsonSerializer.Serialize(personobj);
            using (TextWriter text = new StreamWriter("D:\\abc.json")){
                text.Write(jsontext);
            }
            Console.WriteLine("object serialized");
            Console.WriteLine("Starting Deserialization...");
            using (TextReader text = new StreamReader("D:\\abc.json"))
            {
                person personobj1 = JsonSerializer.Deserialize<person>(text.ReadToEnd());
                Console.WriteLine("Object deserialized.");
                Console.WriteLine("person's name is {0} with age {1}", personobj1.name, personobj1.age);
            }            
        }

Leave a Reply

Your email address will not be published. Required fields are marked *