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.

Add Comment

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