C# Json 시리얼화 및 역시리얼화 1

 public class JsonSerializer
    {
        /// <summary>
        /// json   
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string JsonStringSerializer<T>(T t)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream())
            {
                ser.WriteObject(ms, t);
                string json = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return json;
            }

        }
        /// <summary>
        /// json    
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <returns></returns>
        public static T DeJsonSerializer<T>(string json)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
            {
               object obj=ser.ReadObject(ms);
               ms.Close();
               if (obj == null)
               {
                   throw new NotImplementedException("      NULL,json:" + json);
               }
               return (T)obj;

            }
        }
    }

Josn 시리얼화 및 반시리얼화 데모
C# Json 시리얼화 및 역시리얼화 2

좋은 웹페이지 즐겨찾기