public class JSONConverter<T>
{
public static string toJson(T obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream, obj);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
string dataString = Encoding.UTF8.GetString(dataBytes);
stream.Close();
return dataString;
}
public static T toObject(string str)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
MemoryStream mStream = new MemoryStream(Encoding.Default.GetBytes(str));
T obj = (T)serializer.ReadObject(mStream);
mStream.Close();
return obj;
}
}