Skip to main content

JSON Serialization & Deserialization in c#

 Serialization:

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file.

JSON Serialization:

JSON serialization serializes the public properties of an object into a string, byte array, or stream.

 public static string SerializeObject(object obj)

        {

            string jsonString = null;

            DataContractJsonSerializer serializer = null;

            MemoryStream memorystream = null;

            try

            {

                serializer = new DataContractJsonSerializer(obj.GetType());

                memorystream = new MemoryStream();

                serializer.WriteObject(memorystream, obj);

                jsonString = Encoding.UTF8.GetString(memorystream.ToArray());

            }

            catch

            {

                throw;

            }

            finally

            {

                if (memorystream != null)

                {

                    memorystream.Close();

                    memorystream.Dispose();

                }

            }

            return jsonString;

        }



JSON Deserialization:

It is used to deserialize a JSON string back to an object.

Below is a  generic method that accepts a JSON string and returns an object back.



private static T DeSerializeObject<T>(string jsonString)

        {

            T obj = default(T);

            DataContractJsonSerializer serializer = null;

            MemoryStream memorystream = null;

            try

            {

                serializer = new DataContractJsonSerializer(typeof(T));

                memorystream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

                obj = (T)serializer.ReadObject(memorystream);

            }

            finally

            {

                if (memorystream != null)

                {

                    memorystream.Close();

                    memorystream.Dispose();

                }

            }

            return obj;

        }

Comments

Popular posts from this blog

Gemini software Dot net interview question for experienced

Gemini Interview questions 4-8 year experienced Dot net professional Gemini Interview questions 4-8 year experienced Dot net professional 1,Asp .net mvc request life cycle. 2,How routing works 3,Where codes for routing are written 4,What is attribute based routing in aap.net Mvc 5,Is multiple routes possible on a single action method. 6,What is action filters. 7,what is authentication and authorization filters 8,What are the types of authentication 8,What is the use of data annotation 9,Can we fire data annotation in client side. 10,What is model binding 11,what are Html helpers

15 Essential jQuery Interview Questions

15 Essential jQuery Interview Questions 15 Essential jQuery Interview Questions 1,Explain what the following code will do: $( "div#first, div.first, ol#items > [name$='first']" ) Ans:This code performs a query to retrieve any element with the id first, plus all elements with the class first, plus all elements which are children of the element and whose name attribute ends with the string "first". This is an example of using multiple selectors at once. The function will return a jQuery object containing the results of the query. 2,What is wrong with this code, and how can it be fixed to work properly even with buttons that are added later dynamically? // define the click handler for all buttons $( "button" ).bind( "click", function() {     alert( "Button Clicked!" ) }); /* ... some time later ... */ // dynamically add another button to the page $( "html" ).append( " Click...

ASP .Net MVC

ASp .Net MVC 1. Explain MVC (Model-View-Controller) in general? MVC (Model-View-Controller) is an architectural software pattern that basically decouples various components of a web application. By using MVC pattern, we can develop applications that are more flexible to changes without affecting the other components of our application. §    “Model”, is basically domain data. §    “View”, is user interface to render domain data. §    “Controller”, translates user actions into appropriate operations performed on model. 2. What is ASP.NET MVC? ASP.NET MVC is a web development framework from Microsoft that is based on MVC (Model-View-Controller) architectural design pattern. Microsoft has streamlined the development of MVC based applications using ASP.NET MVC framework. 3. Difference between ASP.NET MVC and ASP.NET WebForms? ASP.NET Web Forms uses Page controller pattern approach for rendering layout, whereas ASP.NET MVC uses Front con...