[C# 문법] MessagePack ObservableCollection 직렬화, 역직렬화



MessagePack ObservableCollection 직렬화, 역직렬화 하기

  • MessagePack에서 Observable Collection 타입과 Class List 타입의 Collection 객체를 직렬화, 역직렬화 가능 여부에 대해서 테스트 진행합니다.
  • Student, User 클래스를 생성합니다.
  • Student 클래스의 속성에는 기본 타입의 속성들과, ObservalbeCollection 타입의 속성과, List 클래스 리스트 속성들을 포함하고 있습니다.
  • Student 객체를 생성하여 직렬화, 역직렬화 테스트 진행합니다.


예제 코드

using MessagePack;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Reflection;

namespace BenchMarkTest
{
    class Program
    {
        static void Main(string[] args)
        {
            SerializeMessagePack();
            DeSerializeMessagePack();
        }

        public static void SerializeMessagePack()
        {
            Student config = CreateMessagePack();

            byte[] bytes = MessagePackSerializer.Serialize(config);

            var json = MessagePackSerializer.ConvertToJson(bytes);

            Console.WriteLine($"직렬화");
            Console.WriteLine(json);
            Console.WriteLine();
        }

        public static void DeSerializeMessagePack()
        {
            Student config = CreateMessagePack();

            // 직렬화, 반환값은 바이트 배열
            var binConfig = MessagePackSerializer.Serialize(config);
            Student binConfig2 = MessagePackSerializer.Deserialize<Student>(binConfig);

            Console.WriteLine($"역직렬화");

            foreach (PropertyInfo property in binConfig2.GetType().GetProperties())
            {
                Console.WriteLine($"{property.Name} : {property.GetValue(binConfig2, null)}");
            }

            PrintToCollectionTypePropertyInfo<string>(binConfig2, "process");
            PrintToCollectionTypePropertyInfo<string>(binConfig2, "systems");
            PrintToCollectionTypePropertyInfo<User>(binConfig2, "users");
        }

        public static void PrintToCollectionTypePropertyInfo<T>(Student target, string listName)
        {
            PropertyInfo propertyInfo = target.GetType().GetProperty(listName.ToString());
            object list = propertyInfo.GetValue(target, null);

            if (string.Compare(listName.ToString(), "users") == 0)
            {
                List<User> details = (List<User>)list;

                foreach (User value in details)
                {
                    Console.WriteLine($"{value.Name} : {value.Age}");
                }
            }
            else
            {
                ObservableCollection<T> details = (ObservableCollection<T>)list;

                foreach (T value in details)
                {
                    Console.WriteLine($"{value}");
                }
            }
        }

        public static Student CreateMessagePack()
        {
            Student myConfig = new Student()
            {
                Name = "범범조조",
                Age = 29,
                Score = 100,
                Grade = "A+"
            };

            myConfig.process = new ObservableCollection<string>();
            for (int index = 0; index < 5; index++)
            {
                myConfig.process.Add($"processID_{index}");
            }

            myConfig.systems = new ObservableCollection<string>();
            for (int index = 0; index < 5; index++)
            {
                myConfig.systems.Add($"systemID_{index}");
            }

            myConfig.users = new List<User>();
            for (int index = 0; index < 5; index++)
            {
                User user = new User()
                {
                    Name = $"Kim_{index}",
                    Age = 20 + index
                };

                myConfig.users.Add(user);
            }

            return myConfig;
        }
    }

    [MessagePackObject]
    public class Student
    {
        [Key("process")]
        public ObservableCollection<string> process { get; set; }
        [Key("systems")]
        public ObservableCollection<string> systems { get; set; }
        [Key("users")]
        public List<User> users { get; set; }
        [Key("Name")]
        public string Name { get; set; }
        [Key("Age")]
        public int Age { get; set; }
        [Key("Score")]
        public int Score { get; set; }
        [Key("Grade")]
        public string Grade { get; set; }
    }

    [MessagePackObject]
    public class User
    {
        [Key("Name")]
        public string Name { get; set; }
        [Key("Age")]
        public int Age { get; set; }
    }
}


실행 결과

직렬화
{"process":["processID_0","processID_1","processID_2","processID_3","processID_4"],"systems":["systemID_0","systemID_1","systemID_2","systemID_3","systemID_4"],"users":[{"Name":"Kim_0","Age":20},{"Name":"Kim_1","Age":21},{"Name":"Kim_2","Age":22},{"Name":"Kim_3","Age":23},{"Name":"Kim_4","Age":24}],"Name":"범범조조","Age":29,"Score":100,"Grade":"A+"}

역직렬화
process : System.Collections.ObjectModel.ObservableCollection`1[System.String]
systems : System.Collections.ObjectModel.ObservableCollection`1[System.String]
users : System.Collections.Generic.List`1[BenchMarkTest.User]
Name : 범범조조
Age : 29
Score : 100
Grade : A+
processID_0
processID_1
processID_2
processID_3
processID_4
systemID_0
systemID_1
systemID_2
systemID_3
systemID_4
Kim_0 : 20
Kim_1 : 21
Kim_2 : 22
Kim_3 : 23
Kim_4 : 24


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY