The below code will serialize an object into JSON and deserialize that JSON back into the object using Generic Method.
Adding the Namespace
using
System.Runtime.Serialization.Json;
Generic Json Serialization
public
static string
JsonSerializer<T>(T t)
{
DataContractJsonSerializer ser =
new
DataContractJsonSerializer(typeof(T));
MemoryStream ms =
new MemoryStream();
ser.WriteObject(ms, t);
string jsonString =
Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return jsonString;
}

Join the conversation! Your thoughts help the community grow.