protobuf-net学习笔记
最近一直在做windows phone 7手机上面的应用程序,学到了一些除了微软这套东西以外的知识,除了Funambol这个库以外(等到完全研究懂了再写笔记),还有protobuf-net这个对数据进行序列化的工具。
其实protobuf-net只是protobuf在.net平台上的应用,而protobuf是google提供的供数据序列化的工具,在网上搜索能找到一大堆相关资料,估计这个工具对数据的处理速度和节省空间的能力都很强,所以很多人才用这个工具。在http://code.google.com/p/protobuf/ 上面有详细的说明,加上我对JAVA不感兴趣,在此就不做什么介绍了。
至于protobuf-net,则是由一个人进行开发的,这个程序员在自己的博客上贴出了很多资料,告诉人们怎么使用,地址在这里:http://marcgravell.blogspot.com/search/label/protobuf-net (需FQ)http://code.google.com/p/protobuf-net/(这里是代码维护地址)
下面来说说如何使用
下载压缩包,解压后会发现有很多平台,至于我就需要选择windows phone平台,里面有三个文件
先把protobuf-net.dll 添加到工程引用
编写自定义的protobuf类,在我的程序中类(ProtoBuf.cs)如下:
- using ProtoBuf;
- using System.Collections.Generic;
-
- namespace SyncControls
- {
- [ProtoContract]
- public class PBCalendarItem
- {
-
- [ProtoMember(1,IsRequired=true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = true)]
- public string title { get; set; }
-
-
- [ProtoMember(3, IsRequired = true)]
- public long created { get; set; }
-
-
- [ProtoMember(4, IsRequired = true)]
- public long modified { get; set; }
-
-
- [ProtoMember(5, IsRequired = false)]
- public long owner_id { get; set; }
-
-
- [ProtoMember(6, IsRequired = false)]
- public string location { get; set; }
-
-
- [ProtoMember(7, IsRequired = false)]
- public string description { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public string timezone { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public int is_primary { get; set; }
-
-
- [ProtoMember(10, IsRequired = false)]
- public int is_public { get; set; }
-
-
- [ProtoMember(11, IsRequired = false)]
- public int pub_status_only { get; set; }
-
-
- [ProtoMember(12, IsRequired = false)]
- public int searchable { get; set; }
-
-
- [ProtoMember(13, IsRequired = false)]
- public string data_domain { get; set; }
-
-
- [ProtoMember(14, IsRequired = false)]
- public int calendar_type { get; set; }
-
-
- [ProtoMember(15, IsRequired = false)]
- public string sync_state { get; set; }
-
-
- [ProtoMember(16, IsRequired = false)]
- public int access_type { get; set; }
-
-
- [ProtoMember(17, IsRequired = false)]
- public string uuid { get; set; }
-
-
- [ProtoMember(18, IsRequired = false)]
- public string color { get; set; }
- }
-
- [ProtoContract]
- public class PBScheduleItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = true)]
- public long calendar_id { get; set; }
-
-
- [ProtoMember(3, IsRequired = true)]
- public long created { get; set; }
-
-
- [ProtoMember(4, IsRequired = true)]
- public long modified { get; set; }
-
-
- [ProtoMember(5, IsRequired = true)]
- public string calendar_type { get; set; }
-
-
- [ProtoMember(6, IsRequired = false)]
- public string sync_state { get; set; }
-
-
- [ProtoMember(7, IsRequired = false)]
- public long owner_id { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public long start_time { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public int duration { get; set; }
-
-
- [ProtoMember(10, IsRequired = false)]
- public int allday_event { get; set; }
-
-
- [ProtoMember(11, IsRequired = false)]
- public int access_type { get; set; }
-
-
- [ProtoMember(12, IsRequired = false)]
- public int status_busy { get; set; }
-
-
- [ProtoMember(13, IsRequired = false)]
- public string title { get; set; }
-
-
- [ProtoMember(14, IsRequired = false)]
- public string location { get; set; }
-
-
- [ProtoMember(15, IsRequired = false)]
- public string description { get; set; }
-
-
- [ProtoMember(16, IsRequired = false)]
- public string url { get; set; }
-
-
- [ProtoMember(17, IsRequired = false)]
- public int check_completed { get; set; }
-
-
- [ProtoMember(18, IsRequired = false)]
- public int repeat_type { get; set; }
-
-
- [ProtoMember(19, IsRequired = false)]
- public int repeat_finished { get; set; }
-
-
- [ProtoMember(20, IsRequired = false)]
- public long repeat_stop_time { get; set; }
-
-
- [ProtoMember(21, IsRequired = false)]
- public int repeat_count { get; set; }
-
-
- [ProtoMember(22, IsRequired = false)]
- public int repeat_frequency { get; set; }
-
-
- [ProtoMember(23, IsRequired = false)]
- public string repeat_month { get; set; }
-
-
- [ProtoMember(24, IsRequired = false)]
- public string repeat_monthday { get; set; }
-
-
- [ProtoMember(25, IsRequired = false)]
- public string repeat_day { get; set; }
-
-
- [ProtoMember(26, IsRequired = false)]
- public string repeat_weekno { get; set; }
-
-
- [ProtoMember(27, IsRequired = false)]
- public string repeat_yearday { get; set; }
-
-
- [ProtoMember(28, IsRequired = false)]
- public string timezone { get; set; }
-
-
- [ProtoMember(29, IsRequired = false)]
- public string uuid { get; set; }
-
-
- [ProtoMember(30, IsRequired = false)]
- public string calendar_uuid { get; set; }
-
- [ProtoMember(31, IsRequired = false)]
- public List<PBScheduleExceptionItem> scheduleExceptions { get; private set; }
-
- [ProtoMember(32, IsRequired = false)]
- public List<PBScheduleAlarmItem> scheduleAlarms { get; private set; }
-
-
- [ProtoMember(33, IsRequired = false)]
- public List<PBScheduleUserItem> scheduleUsers { get; private set; }
-
-
- [ProtoMember(34, IsRequired = false)]
- public List<PBScheduleShareItem> scheduleShares { get; private set; }
-
- public PBScheduleItem()
- {
- scheduleExceptions = new List<PBScheduleExceptionItem>();
- scheduleAlarms = new List<PBScheduleAlarmItem>();
- scheduleShares = new List<PBScheduleShareItem>();
- scheduleUsers = new List<PBScheduleUserItem>();
- }
- }
-
- [ProtoContract]
- public class PBScheduleExceptionItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = true)]
- public int schedule_id { get; set; }
-
-
- [ProtoMember(3, IsRequired = true)]
- public string excep_date { get; set; }
-
-
- [ProtoMember(4, IsRequired = false)]
- public string sync_state { get; set; }
- }
-
- [ProtoContract]
- public class PBScheduleAlarmItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = false)]
- public long created { get; set; }
-
-
- [ProtoMember(3, IsRequired = false)]
- public long modified { get; set; }
-
-
- [ProtoMember(4, IsRequired = true)]
- public long schedule_id { get; set; }
-
-
- [ProtoMember(5, IsRequired = true)]
- public long user_id { get; set; }
-
-
- [ProtoMember(6, IsRequired = true)]
- public long alarm_id { get; set; }
-
-
- [ProtoMember(7, IsRequired = true)]
- public int before_minutes { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public long next_alarm { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public string sync_state { get; set; }
- }
-
- [ProtoContract]
- public class PBScheduleUserItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = true)]
- public long schedule_id { get; set; }
-
-
- [ProtoMember(3, IsRequired = true)]
- public long user_id { get; set; }
-
-
- [ProtoMember(4, IsRequired = true)]
- public int is_owner { get; set; }
-
-
- [ProtoMember(5, IsRequired = true)]
- public int participant_status { get; set; }
-
-
- [ProtoMember(6, IsRequired = false)]
- public int allow_edit { get; set; }
-
-
- [ProtoMember(7, IsRequired = false)]
- public int allow_invite_others { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public int allow_view_participants { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public string participant_comment { get; set; }
-
-
- [ProtoMember(10, IsRequired = false)]
- public string sync_state { get; set; }
- }
-
- [ProtoContract]
- public class PBScheduleShareItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = false)]
- public long created { get; set; }
-
-
- [ProtoMember(3, IsRequired = false)]
- public long modified { get; set; }
-
-
- [ProtoMember(4, IsRequired = false)]
- public string sync_state { get; set; }
-
-
- [ProtoMember(5, IsRequired = true)]
- public long schedule_id { get; set; }
-
-
- [ProtoMember(6, IsRequired = true)]
- public long contact_id { get; set; }
-
-
- [ProtoMember(7, IsRequired = false)]
- public string fullname { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public string mobile { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public string send_state { get; set; }
- }
-
- [ProtoContract]
- public class PBNoteItem
- {
-
- [ProtoMember(1, IsRequired = true)]
- public long pid { get; set; }
-
-
- [ProtoMember(2, IsRequired = true)]
- public long created { get; set; }
-
-
- [ProtoMember(3, IsRequired = true)]
- public long modified { get; set; }
-
-
- [ProtoMember(4, IsRequired = false)]
- public string sync_state { get; set; }
-
-
- [ProtoMember(5, IsRequired = false)]
- public string title { get; set; }
-
-
- [ProtoMember(6, IsRequired = false)]
- public long owner_id { get; set; }
-
-
- [ProtoMember(7, IsRequired = false)]
- public int state { get; set; }
-
-
- [ProtoMember(8, IsRequired = false)]
- public int priority { get; set; }
-
-
- [ProtoMember(9, IsRequired = false)]
- public long finish_time { get; set; }
-
-
- [ProtoMember(10, IsRequired = false)]
- public long deadline { get; set; }
-
-
- [ProtoMember(11, IsRequired = false)]
- public string tags { get; set; }
-
-
- [ProtoMember(12, IsRequired = false)]
- public int seq { get; set; }
-
-
- [ProtoMember(13, IsRequired = false)]
- public string uuid { get; set; }
- }
- }
再编写转换类,这里是PBConverter.cs
使用的时候,只需要按照下面方式写即可
- protected override SyncItem GetItemContent(SyncItem item)
- {
- CalendarItem t = CalendarManager.GetCalendarByID(Convert.ToInt64(item.Key));
- PBCalendarItem pbItem = t.ToProtoBuf();
- return ToSyncItem(pbItem);
- }
-
- private SyncItem ToSyncItem(PBCalendarItem pbItem)
- {
- SyncItem item = new SyncItem(Convert.ToString(pbItem.pid));
- string syncState = pbItem.sync_state;
- MemoryStream stream = new MemoryStream();
-
- Serializer.Serialize<PBCalendarItem>(stream, pbItem);
- item.SetContent(stream.GetBuffer());
- return item;
- }
-
- private PBCalendarItem FromSyncItem(SyncItem item)
- {
- byte[] b = item.GetContent();
- PBCalendarItem pbItem = null;
- try
- {
- MemoryStream stream = new MemoryStream(b);
-
- pbItem = Serializer.Deserialize<PBCalendarItem>(stream);
- }
- catch { }
- return pbItem;
- }
这里是把数据写入到一个内存流里面,而不是通常意义上的文件之中,如果是文件可以看这里的例子
http://hi.baidu.com/jack1865/item/09a3f8ef4c8505f2eb34c957
以上只是对这个工具的简单使用说明而已,现在protobuf-net的作者已经开发出了更强的序列号工具,通过他给的precompiler工具加上自己写的特定框架下的试验工程,编译得到适合该特定框架下的序列化工具。
precompile {some path}\PhoneDto.dll –o:PhoneSerializer.dll –t:MySerializer
Then, just use MySerializer
:
var ser = new MySerializer();
ser.Serialize(output, obj);
看来还有很多知识要学习。