■ JsonConverter 클래스 : MemoryStream 직렬화/역직렬화하기

----------------------------------------------------------------------------------------------------


TestProject.zip


MemoryStreamJSONConverter.cs

 

 

using System;

using System.IO;

 

using Newtonsoft.Json;

 

namespace TestJSONStream

{

    /// <summary>

    /// 메모리 스트림 JSON 컨버터

    /// </summary>

    public class MemoryStreamJSONConverter : JsonConverter

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 변환 가능 여부 구하기 - CanConvert(objectType)

 

        /// <summary>

        /// 변환 가능 여부 구하기

        /// </summary>

        /// <param name="objectType">객체 타입</param>

        /// <returns>변환 가능 여부</returns>

        public override bool CanConvert(Type objectType)

        {

            return typeof(MemoryStream).IsAssignableFrom(objectType);

        }

 

        #endregion

        #region JSON 읽기 - ReadJson(jsonReader, objectType, existingValue, jsonSerializer)

 

        /// <summary>

        /// JSON 읽기

        /// </summary>

        /// <param name="jsonReader">JSON 리더</param>

        /// <param name="objectType">객체 타입</param>

        /// <param name="existingValue">기존 값</param>

        /// <param name="jsonSerializer">JSON 직렬화기</param>

        /// <returns>객체</returns>

        public override object ReadJson(JsonReader jsonReader, Type objectType, object existingValue, JsonSerializer jsonSerializer)

        {

            byte[] byteArray = jsonSerializer.Deserialize<byte[]>(jsonReader);

 

            return byteArray != null ? new MemoryStream(byteArray) : new MemoryStream();

        }

 

        #endregion

        #region JSON 쓰기 - WriteJson(jsonWriter, value, jsonSerializer)

 

        /// <summary>

        /// JSON 쓰기

        /// </summary>

        /// <param name="jsonWriter">JSON 라이터</param>

        /// <param name="value"></param>

        /// <param name="jsonSerializer">JSON 직렬화기</param>

        public override void WriteJson(JsonWriter jsonWriter, object value, JsonSerializer jsonSerializer)

        {

            byte[] byteArray = ((MemoryStream)value).ToArray();

 

            jsonSerializer.Serialize(jsonWriter, byteArray);

        }

 

        #endregion

    }

}

 

 

Program.cs

 

 

using System;

using System.IO;

using Newtonsoft.Json;

 

namespace TestJSONStream

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            string source = "Test string";

 

            Console.WriteLine("소스 문자열 : {0}", source);

 

            string json = GetJSON(source);

 

            Console.WriteLine("JSON 문자열 : {0}", json);

            

            string target = GetString(json);

            

            Console.WriteLine("타겟 문자열 : {0}", target);

            

            Console.Read();

        }

 

        #endregion

 

        #region JSON 구하기 - GetJSON(source)

 

        /// <summary>

        /// JSON 구하기

        /// </summary>

        /// <param name="source">소스 문자열</param>

        /// <returns>JSON 문자열</returns>

        public static string GetJSON(string source)

        {

            MemoryStream memoryStream = new MemoryStream();

            

            StreamWriter streamWriter = new StreamWriter(memoryStream);

            

            streamWriter.WriteLine(source);

            

            streamWriter.Flush();

            

            memoryStream.Position = 0;

            

            string json = JsonConvert.SerializeObject(memoryStream, Formatting.None, new MemoryStreamJSONConverter());

 

            return json;

        }

 

        #endregion

        #region 문자열 구하기 - GetString(json)

 

        /// <summary>

        /// 문자열 구하기

        /// </summary>

        /// <param name="json">JSON 문자열</param>

        /// <returns>문자열</returns>

        public static string GetString(string json)

        {

            MemoryStream memoryStream = JsonConvert.DeserializeObject<MemoryStream>(json, new MemoryStreamJSONConverter());

            

            StreamReader streamReader = new StreamReader(memoryStream);

            

            string target = streamReader.ReadLine();

 

            return target;

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG