■ JsonConverter 클래스를 사용해 MemoryStream을 직렬화/역직렬화하는 방법을 보여준다.
▶ MemoryStreamJSONConverter.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
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() { Console.Title = "JsonConverter 클래스 : MemoryStream 직렬화/역직렬화하기"; 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 } } |