/// <summary>
/// JSON 타입의 직렬화
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serialize<T>(T obj)
{
string json = null;
using (MemoryStream ms = new System.IO.MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
serializer.WriteObject(ms, obj);
json = Encoding.UTF8.GetString(ms.ToArray());
}
return json;
}
/// <summary>
/// JSON 타입의 역직렬화
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json"></param>
/// <returns></returns>
public static T Deserialize<T>(string json)
{
T obj = Activator.CreateInstance<T>();
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
obj = (T)serializer.ReadObject(ms);
}
return obj;
}
[출처] JSON 타입의 직렬화 / 역직렬화|작성자 조데스