using Newtonsoft.Json;
using System.Runtime.Serialization.Json;
using System.IO;
public static T JsonToObject<T>(string jsonString)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
T jsonObject = (T)ser.ReadObject(ms);
ms.Close();
return jsonObject;
}
public static string ObjectToJson<T>(T jsonObject)
{
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, jsonObject);
// T jo = (T)ser.ReadObject(ms);
System.IO.StreamReader reader = new StreamReader(ms);
ms.Position = 0;
string strRes = reader.ReadToEnd();
reader.Close();
ms.Close();
return strRes;
}