Skip to content

Generic c# Json serializer/deserializer sample

  1.  
  2. public class JsonSerializer
  3.     {
  4.         /// <summary>
  5.         /// Serializes a given object into a json string
  6.         /// </summary>
  7.         /// <typeparam name="T">Type to serialize</typeparam>
  8.         /// <param name="objectToSerialize">Object to serialize</param>
  9.         /// <returns>Serialized Json string of the given object</returns>
  10.         public static string SerializeToJson<T>(T objectToSerialize) where T : class
  11.         {
  12.             if(objectToSerialize==null)
  13.             {
  14.                 throw new ArgumentException("objectToSerialize must not be null");
  15.             }
  16.             MemoryStream ms = null;
  17.             try
  18.             {
  19.                 DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
  20.                 ms = new MemoryStream();
  21.                 serializer.WriteObject(ms, objectToSerialize);
  22.                 return Encoding.UTF8.GetString(ms.ToArray());
  23.             }
  24.             finally
  25.             {
  26.                 if (ms != null)
  27.                 {
  28.                     ms.Close();
  29.                 }
  30.             }
  31.         }
  32.  
  33.         /// <summary>
  34.         /// Deserialize a given json string
  35.         /// </summary>
  36.         /// <typeparam name="T">Type to deserialize</typeparam>
  37.         /// <param name="jsonStringToDeserialize">Json String representation</param>
  38.         /// <returns>Deserialized object</returns>
  39.         public static T DeserializeFromJson<T>(string jsonStringToDeserialize) where T : class
  40.         {
  41.             if(string.IsNullOrEmpty(jsonStringToDeserialize))
  42.             {
  43.                 throw new ArgumentException("jsonStringToDeserialize must not be null");
  44.             }
  45.             MemoryStream ms = null;
  46.             try
  47.             {
  48.                 ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonStringToDeserialize));
  49.                 DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
  50.                 return (T)serializer.ReadObject(ms);
  51.             }
  52.             finally
  53.             {
  54.                 if (ms != null)
  55.                     ms.Close();
  56.             }
  57.         }
  58.     }
  59.  

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

is edubirdie good on :

Computers are playing vital role in many fields of life. By edubirdie services people can run programs on computers. They can use different software in their computers. They can install software and can use them for good purposes.

Patricia Sperry on :

For serialization and deserialization, I have used this c# generic method. This codes works for only to serialize to JSON. I also wanted to deserialize objects into generic method back. I also use c# in my movies leather jackets website development but haven’t found any issue in the code like this.

noracristian on :

This coding may be performed openly both classes of deserialization and serialization in a public mode as well coding of sample is properly aligned hope no error will found on run time. We use different libraries in creating our page black friday sale on jackets in which we declare if else function on ad cart method that is working perfectly.

Ihosts on :

Very useful post. This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. Really its great article. Keep it up.

Thriller jacket on :

The post is very informative and the sample is very helpful for me. And many people want to read this type of post I am also part of them and after the job, my first choice is reading this type of post.

Assignment Writing Services on :

I really appreciate this type of working and interested to see more good posts in the coming future. Actually it's a very nice site! this is my first visit on this site my friend gave the link of this site! and the site is really great.

research proposal writing service on :

coding is not so easy this is all about the coding material and elastic things, the code is very difficult we should apply minimum number of code of lines. because the developer is facing difficulties.

Drywall Company on :

Thanks for this blog. I am not very computer savvy and am always trying to learn more! www.billingsdrywallcompany.com

C_TS4C_2020 dumps on :

CTS4C2020 - SAP Certified Application Associate - SAP S/4HANA Cloud-essentials edition implementation with SAP Activate.

Add Comment

You can use [geshi lang=lang_name [,ln={y|n}]][/geshi] tags to embed source code snippets.
Markdown format allowed
Form options