{"id":292,"date":"2020-10-14T06:19:03","date_gmt":"2020-10-14T06:19:03","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=292"},"modified":"2020-10-14T06:19:03","modified_gmt":"2020-10-14T06:19:03","slug":"serialization-and-deserialization-in-c","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2020\/10\/14\/serialization-and-deserialization-in-c\/","title":{"rendered":"Serialization and Deserialization in C#"},"content":{"rendered":"\n<h2>What is Serialization in C#?<\/h2>\n\n\n\n<p>&nbsp;Serialization in C# is the process of bringing an object into a form that it can be written on stream. It&#8217;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.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/www.c-sharpcorner.com\/article\/serialization-and-deserialization-in-c-sharp\/Images\/Serialization.PNG\" alt=\"serialization\"\/><\/figure>\n\n\n\n<h2>What is Deserialization in C#?<\/h2>\n\n\n\n<p>\u00a0As 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.\u00a0<\/p>\n\n\n\n<p><strong>Types<\/strong><\/p>\n\n\n\n<ul><li>Binary Serialization<\/li><li>XML Serialization<\/li><li>JSON Serialization&nbsp;<\/li><\/ul>\n\n\n\n<p><strong>Example<\/strong>\u00a0Here I will be giving you an example of how to serialize and deserialize an object using binary formatter or xml formatter.<\/p>\n\n\n\n<p>Binary:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Serializable]\r\n         \/*used to make the class serializable. If you don't mention this attribute, \r\n          you will get an error when you try to serialize the class.\r\n          if you dont want to serialise something use &#91;NonSerialized]\r\n         *\/\r\n        public class person\r\n        {\r\n            public string name { get; set; }\r\n            public int age { get; set; }\r\n        }\r\n        static void Main(string&#91;] args)\r\n        {\r\n            person personobj = new person() { name = \"sumit\", age = 25 };\r\n            IFormatter formattable = new BinaryFormatter();\r\n            Stream stream = new FileStream(\"D:\\\\abc.txt\", FileMode.Create, FileAccess.Write);\r\n            formattable.Serialize(stream, personobj);\r\n            stream.Close();\r\n            Console.WriteLine(\"object serialized\");\r\n            Console.WriteLine(\"Starting Deserialization...\");\r\n            stream = new FileStream(\"D:\\\\abc.txt\", FileMode.Open, FileAccess.Read);\r\n            person personobj1 = (person)formattable.Deserialize(stream);\r\n            Console.WriteLine(\"Object deserialized.\");\r\n            Console.WriteLine(\"person's name is {0} with age {1}\",personobj1.name,personobj1.age);\r\n        }<\/code><\/pre>\n\n\n\n<p>XML:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class person\r\n        {\r\n            public string name { get; set; }\r\n            public int age { get; set; }\r\n        }\r\n        public static void Main(string&#91;] args)\r\n        {\r\n            person personobj = new person() { name = \"sumit\", age = 25 };\r\n            XmlSerializer xmlSerializer = new XmlSerializer(typeof(person));\r\n            using (TextWriter text = new StreamWriter(\"D:\\\\abc.xml\")){\r\n                xmlSerializer.Serialize(text, personobj);\r\n            }\r\n            Console.WriteLine(\"object serialized\");\r\n            Console.WriteLine(\"Starting Deserialization...\");\r\n            using (TextReader text = new StreamReader(\"D:\\\\abc.xml\"))\r\n            {\r\n                person personobj1 = (person)xmlSerializer.Deserialize(text);\r\n                Console.WriteLine(\"Object deserialized.\");\r\n                Console.WriteLine(\"person's name is {0} with age {1}\", personobj1.name, personobj1.age);\r\n            }            \r\n        }<\/code><\/pre>\n\n\n\n<p>JSON: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class person\r\n        {\r\n            public string name { get; set; }\r\n            public int age { get; set; }\r\n        }\r\n        public static void Main(string&#91;] args)\r\n        {\r\n            person personobj = new person() { name = \"sumit\", age = 25 };\r\n            string jsontext = JsonSerializer.Serialize(personobj);\r\n            using (TextWriter text = new StreamWriter(\"D:\\\\abc.json\")){\r\n                text.Write(jsontext);\r\n            }\r\n            Console.WriteLine(\"object serialized\");\r\n            Console.WriteLine(\"Starting Deserialization...\");\r\n            using (TextReader text = new StreamReader(\"D:\\\\abc.json\"))\r\n            {\r\n                person personobj1 = JsonSerializer.Deserialize&lt;person>(text.ReadToEnd());\r\n                Console.WriteLine(\"Object deserialized.\");\r\n                Console.WriteLine(\"person's name is {0} with age {1}\", personobj1.name, personobj1.age);\r\n            }            \r\n        }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Serialization in C#? &nbsp;Serialization in C# is the process of bringing an object into a form that it can be written on stream. It&#8217;s the process of converting the object into a form&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/292"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=292"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/292\/revisions"}],"predecessor-version":[{"id":293,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/292\/revisions\/293"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=292"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}