허당 레몬도리

/// <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;

        }


profile

허당 레몬도리

@LemonDory

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!