첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

▶ Program.cs

using System;

class Program
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Private

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

    /// <summary>
    /// 프로그램 시작하기
    /// </summary>
    private static void Main()
    {
        Employee employee1 = new Employee() { ID = "1", Name = "홍길동" };

        string json = JSONManager.Serialize<Employee>(employee1);

        Console.WriteLine(json);


        Employee employee2 = JSONManager.Deserialize<Employee>(json);

        Console.WriteLine(employee2.Name);
    }
}

 

▶ Employee.cs

/// <summary>
/// 직원
/// </summary>
public class Employee
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Property
    ////////////////////////////////////////////////////////////////////////////////////////// Public

    #region ID - ID

    /// <summary>
    /// ID
    /// </summary>
    public string ID { get; set; }

    #endregion

    #region 성명 - Name

    /// <summary>
    /// 성명
    /// </summary>
    public string Name { get; set; }

    #endregion
}

 

▶ JSONManager.cs

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

/// <summary>
/// JSON 관리자
/// </summary>
public class JSONManager
{
    //////////////////////////////////////////////////////////////////////////////////////////////////// Method
    ////////////////////////////////////////////////////////////////////////////////////////// Static
    //////////////////////////////////////////////////////////////////////////////// Public

    #region 직렬화 하기 - Serialize<TSourceObject>(sourceObject)

    /// <summary>
    /// 직렬화 하기
    /// </summary>
    /// <typeparam name="TSourceObject">소스 객체 타입</typeparam>
    /// <param name="sourceObject">소스 객체</param>
    /// <returns>직렬화 문자열</returns>
    public static string Serialize<TSourceObject>(TSourceObject sourceObject)
    {
        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(sourceObject.GetType());

        MemoryStream memoryStream = new MemoryStream();

        dataContractJsonSerializer.WriteObject(memoryStream, sourceObject);

        string result = Encoding.UTF8.GetString(memoryStream.ToArray());

        return result;
    }

    #endregion

    #region 역직렬화 하기 - Deserialize<TTargetObject>(jsonString)

    /// <summary>
    /// 역직렬화 하기
    /// </summary>
    /// <typeparam name="TTargetObject">타겟 객체 타입</typeparam>
    /// <param name="jsonString">JSON 문자열</param>
    /// <returns>객체</returns>
    public static TTargetObject Deserialize<TTargetObject>(string jsonString)
    {
        TTargetObject targetObject = Activator.CreateInstance<TTargetObject>();

        MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));

        DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(targetObject.GetType());

        targetObject = (TTargetObject)dataContractJsonSerializer.ReadObject(memoryStream);

        memoryStream.Close();

        return targetObject;
    }

    #endregion
}

 

728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요