c#序列化与发序列化之protobuf-net篇

c#中的序列化是指将对象写入流的过程,反序列话是指将对象从流中读取的过程。

protobuf-net:

Protobuf是google开源的一个项目,用户数据序列化反序列化,google声称google的数据通信都是用该序列化方法。它比xml格式要少的多,甚至比二进制数据格式也小的多。

Protobuf格式协议和xml一样具有平台独立性,可以在不同平台间通信,通信所需资源很少,并可以扩展,可以旧的协议上添加新数据

 

ProtobufHelper类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using ProtoBuf;
 7 
 8 namespace Protobuf_Ne
 9 {
10     class ProtobufHelper
11     {
12         /// <summary>
13         /// 序列化
14         /// </summary>
15         /// <typeparam name="T"></typeparam>
16         /// <param name="path">文件地址</param>
17         /// <param name="t">数据集合</param>
18         public static void Serialize<T>( string path,T t)
19         {
20             //写入本地文件
21             using (Stream file = File.Create(path))
22             {
23                 Serializer.Serialize<T>(file, t);
24                 file.Close();
25             }
26             //写入内存
27             //using (MemoryStream ms = new MemoryStream())
28             //{
29             //    Serializer.Serialize<T>(ms, t);
30             //    Serializer.Serialize<T>(file, t); 
31             //    return Encoding.UTF8.GetString(ms.ToArray());
32             //}
33         }
34         /// <summary>
35         /// 反序列化
36         /// </summary>
37         /// <typeparam name="T"></typeparam>
38         /// <param name="path">文件地址</param>
39         /// <returns></returns>
40         public static T DeSerialize<T>(string path)
41         {
42             using (Stream file = File.OpenRead(path))
43             {
44                 return Serializer.Deserialize<T>(file);
45             }
46             //读取内存中的数据
47             //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(content)))
48             //{
49             //    T t = Serializer.Deserialize<T>(ms);
50             //    return t;
51             //}
52         }
53     }
54 }
View Code

//简单实例

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.IO;
10 using ProtoBuf;
11 using System.Web.Script.Serialization;
12 using Newtonsoft.Json;
13 namespace Protobuf_Ne
14 {
15     public partial class Form1 : Form
16     {
17         public Form1()
18         {
19             InitializeComponent();
20         }
21 
22         private void Form1_Load(object sender, EventArgs e)
23         {
24              List<User> list = new List<User>(); 
25             for (int i = 0; i < 1000; i++) 
26             { 
27                 list.Add(new User() { UserID = i, UserName = "u" + i.ToString() }); 
28             }
29 
30             //json 
31             string path2 = AppDomain.CurrentDomain.BaseDirectory + "json.txt"; 
32             JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer(); 
33             string json = jss.Serialize(list);
34             File.WriteAllText(path2, json);
35 
36             //protobuff 
37             string path = AppDomain.CurrentDomain.BaseDirectory + "protobuf.txt";
38             ProtobufHelper.Serialize(path,list);
39         }
40 
41         [ProtoContract]
42         public class User
43         {
44             [ProtoMember(1, IsRequired = true)]
45             public int UserID { get; set; }
46 
47             [ProtoMember(2, IsRequired = true)]
48             public string UserName { get; set; }
49 
50         }
51 
52         private void button1_Click(object sender, EventArgs e)
53         {
54             string path = AppDomain.CurrentDomain.BaseDirectory + "protobuf.txt"; 
55             List<User> list2 = new List<User>();
56             list2 = ProtobufHelper.DeSerialize<List<User>>(path);
57             foreach (User u in list2)
58             {
59               textBox1.Text+= string.Format("UserID={0}, UserName={1}", u.UserID, u.UserName)+"\r\n";
60             }
61         } 
62     
63 
64     }
65 }
View Code

结果:

 

posted @ 2016-03-28 16:05  一生一世的陪伴  阅读(1415)  评论(0编辑  收藏  举报