C# 使用 protobuf 进行对象序列化与反序列化

来源:互联网 发布:5年php程序员工资 编辑:程序博客网 时间:2024/05/21 10:52
本文永久地址:http://www.omuying.com/article/148.aspx,【文章转载请注明出处!】

protobuf 是 google的一个开源项目,可用于以下两种用途:(1)数据的存储(序列化和反序列化),类似于xml、json等;(2)制作网络通信协议。源代码下载地址:https://github.com/mgravell/protobuf-net;开源项目地址如下:https://code.google.com/p/protobuf-net/。

protobuf 工具类 DataUtils.cs 代码如下:
01using ProtoBuf;
02using System;
03using System.Collections.Generic;
04 
05/// <summary>
06/// DataUtils 的摘要说明
07/// </summary>
08public class DataUtils
09{
10    public static byte[] ObjectToBytes<T>(T instance)
11    {
12        try
13        {
14            byte[] array;
15            if (instance == null)
16            {
17                array = new byte[0];
18            }
19            else
20            {
21                System.IO.MemoryStream memoryStream = newSystem.IO.MemoryStream();
22                Serializer.Serialize<T>(memoryStream, instance);
23                array = new byte[memoryStream.Length];
24                memoryStream.Position = 0L;
25                memoryStream.Read(array, 0, array.Length);
26                memoryStream.Dispose();
27            }
28            return array;
29        }
30        catch (System.Exception)
31        {
32            return new byte[0];
33        }
34    }
35    public static T BytesToObject<T>(byte[] bytesData, int offset, intlength)
36    {
37        if (bytesData.Length == 0)
38        {
39            return default(T);
40        }
41        try
42        {
43            System.IO.MemoryStream memoryStream = newSystem.IO.MemoryStream();
44            memoryStream.Write(bytesData, 0, bytesData.Length);
45            memoryStream.Position = 0L;
46            T result = Serializer.Deserialize<T>(memoryStream);
47            memoryStream.Dispose();
48            return result;
49        }
50        catch (System.Exception ex)
51        {
52            return default(T);
53        }
54    }
55}
测试用例代码如下:
01[ProtoContract]
02class UserInfo
03{
04    [ProtoMember(1)]
05    public int UserID;
06     
07    [ProtoMember(2)]
08    public string UserName;
09 
10    [ProtoMember(3)]
11    public List<ItemInfo> ItemList;
12 
13    // 默认构造函数必须有,否则反序列化会报 No parameterless constructor found for x 错误!
14    public UserInfo() { }
15 
16    public UserInfo(int userID, string userName)
17    {
18        this.UserID = userID;
19        this.UserName = userName;
20        this.ItemList = new List<ItemInfo>();
21    }
22}
23 
24[ProtoContract]
25class ItemInfo
26{
27    [ProtoMember(1)]
28    public int ItemID;
29 
30    [ProtoMember(2)]
31    public string ItemName;
32 
33    // 默认构造函数必须有,否则反序列化会报 No parameterless constructor found for x 错误!
34    public ItemInfo() { }
35 
36    public ItemInfo(int itemID, string itemName)
37    {
38        this.ItemID = itemID;
39        this.ItemName = itemName;
40    }
41}
42 
43using ProtoBuf;
44using System;
45using System.Collections.Generic;
46using System.Linq;
47using System.Web;
48using System.Web.UI;
49using System.Web.UI.WebControls;
50 
51public partial class _Default : System.Web.UI.Page
52{
53    protected void Page_Load(object sender, EventArgs e)
54    {
55        if (!IsPostBack)
56        {
57            UserInfo userInfo = new UserInfo(1, "user 1");
58            userInfo.ItemList.Add(new ItemInfo(1, "item 1"));
59            userInfo.ItemList.Add(new ItemInfo(2, "item 2"));
60 
61            byte[] userBytes = DataUtils.ObjectToBytes<UserInfo>(userInfo);
62 
63            UserInfo serUserInfo = DataUtils.BytesToObject<UserInfo>(userBytes, 0, userBytes.Length);
64 
65        }
66    }
67}
0 0
原创粉丝点击