zoukankan      html  css  js  c++  java
  • protobufnet 简介

    www.facebuyer.com 买家面对面

          protobuf-net是Google的ProtocolBuffer的.net实现。ProtocolBuffer是用于结构化数据串行化的灵活、高效、自动的方法,有如XML,不过它更小、更快、也更简单。你可以定义自己的数据结构,然后使用代码生成器生成的代码来读写这个数据结构。你甚至可以在无需重新部署程序的情况下更新数据结构。

    引用protobuf-net.dll组件

    1、能过特性定义ProtoBuf格式的类

    [ProtoContract]
    public class User
    {
        [ProtoMember(1, IsRequired = true)]
        public int UserID { get; set; }

        [ProtoMember(2, IsRequired = true)]
        public string UserName { get; set; }

    }

    required表示在这个消息中这个字段是必须的,就像数据库中的非空字段一样的,必须要有值。
    optional表示是可选的,可以有,也可以没有,对应到数据库中的就可空字段。
    repeated表示这个字段是可以重复的,一个消息是可以有一个或多个这个字段的。

    2、序列化:

    using (Stream file = File.Create(path))
    {
        Serializer.Serialize<List<User>>(file, list);
        file.Close();
    }

    3、反序列化:

    List<User> list2 = new List<User>();
    using (Stream file = File.OpenRead(path))
    {
        list2 = Serializer.Deserialize<List<User>>(file);
    }

    4、例子:

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<User> list = new List<User>();
                for (int i = 0; i < 1000; i++)
                {
                    list.Add(new User() { UserID = i, UserName = "u" + i.ToString() });
                }

                //json
                string path2 = AppDomain.CurrentDomain.BaseDirectory + "json.txt";
                System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
                string json = jss.Serialize(list);
                File.WriteAllText(path2, json);

                //protobuff
                string path = AppDomain.CurrentDomain.BaseDirectory + "protobuf.txt";
                using (Stream file = File.Create(path))
                {
                    Serializer.Serialize<List<User>>(file, list);
                    file.Close();
                }

                List<User> list2 = new List<User>();
                using (Stream file = File.OpenRead(path))
                {
                    list2 = Serializer.Deserialize<List<User>>(file);
                }

                foreach (User u in list2)
                {
                    Console.WriteLine(string.Format("UserID={0}, UserName={1}", u.UserID, u.UserName));
                }

                Console.ReadKey();
            }
        }

        [ProtoContract]
        public class User
        {
            [ProtoMember(1, IsRequired = true)]
            public int UserID { get; set; }

            [ProtoMember(2, IsRequired = true)]
            public string UserName { get; set; }

        }
    }

    测试结果:

    json.txt 33k

    protobuf.txt 11k

    大小相差了3倍

  • 相关阅读:
    86. Partition List
    328. Odd Even Linked List
    19. Remove Nth Node From End of List(移除倒数第N的结点, 快慢指针)
    24. Swap Nodes in Pairs
    2. Add Two Numbers(2个链表相加)
    92. Reverse Linked List II(链表部分反转)
    109. Convert Sorted List to Binary Search Tree
    138. Copy List with Random Pointer
    为Unity的新版ugui的Prefab生成预览图
    ArcEngine生成矩形缓冲区
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760305.html
Copyright © 2011-2022 走看看