什么是ProtoBuf-net

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

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

Protobuf是在java和c++运行的,Protobuf-net当然就是Protobuf在.net环境下的移植。

请参见:https://code.google.com/p/protobuf-net/

Get Start

[ProtoBuf.ProtoContract]
public class Person
{
[ProtoBuf.ProtoMember(1)]
public int Id { get; set; }
[ProtoBuf.ProtoMember(2)]
public string Name { get; set; }
[ProtoBuf.ProtoMember(3)]
public Address Address { get; set; }
} [ProtoBuf.ProtoContract]
public class Address
{
[ProtoBuf.ProtoMember(1)]
public string Line1 { get; set; }
[ProtoBuf.ProtoMember(2)]
public string Line2 { get; set; }
}

类前加上ProtoContract Attrbuit,成员加上ProtoMember Attribute即可,其中ProtoMember需要一个大于0的int类型的值,原则上这个int类型没有大小限制,但建议从1开始,这是一个良好的习惯,另外这个参数必需是这个类成员的唯一标识,不可重复

序列化

            var person = new Person
{
Id = 1,
Name = "First",
Address = new Address { Line1 = "Line1", Line2 = "Line2" }
};
using (var file = System.IO.File.Create("Person.bin"))
{
ProtoBuf.Serializer.Serialize(file, person);
}

反序列化

Person newPerson;
using (var file = System.IO.File.OpenRead("Person.bin"))
{
newPerson = ProtoBuf.Serializer.Deserialize<Person>(file);
}
使用起来很简单,代码移植也会相当方便,下面我要对比下序列化的文件大小。
1.使用ProtoBuf序列化1000个对象,查看Person.bin文件大小为:30 KB (29,760 字节)
List<Person> list = new List<Person>();
for (var i = 0; i < 1000; i++)
{
var person = new Person
{
Id = i,
Name = "Name"+i,
Address = new Address { Line1 = "Line1", Line2 = "Line2" }
};
list.Add(person);
} using (var file = System.IO.File.Create("Person.bin"))
{
ProtoBuf.Serializer.Serialize(file, list);
}

2.使用xml序列化1000个对象,Person.xml大小为:152 KB (155,935 字节)

            List<Person> list = new List<Person>();
for (var i = 0; i < 1000; i++)
{
var person = new Person
{
Id = i,
Name = "Name"+i,
Address = new Address { Line1 = "Line1", Line2 = "Line2" }
};
list.Add(person);
} System.Xml.Serialization.XmlSerializer xmlSerizlizer = new System.Xml.Serialization.XmlSerializer(typeof(List<Person>));
using(var file= System.IO.File.Create("Persion.xml")){
xmlSerizlizer.Serialize(file, list);
}

3. 使用binary序列化1000个对象,Person.dat大小为:54.1 KB (55,445 字节)

            List<Person> list = new List<Person>();
for (var i = 0; i < 1000; i++)
{
var person = new Person
{
Id = i,
Name = "Name"+i,
Address = new Address { Line1 = "Line1", Line2 = "Line2" }
};
list.Add(person);
} using(var file = new System.IO.FileStream("Person.dat", System.IO.FileMode.Create))
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(file, list);
}

在这个实验中ProtoBuf比xml序列化小5倍,比二进制也近小一倍,有人说ProtoBuf比xml可以小到20倍,根据数据的复杂度这是有可能的。ProtoBuf的数据格式做为数据报文有着绝对优势,当然也有个弊端,它是2进制报文,没有xml格式这样的可读性,要想看懂报文内容只能用ProtoBuf反序列化了,不过我认识这基本上不是问题~

高效的序列化组件 Protobuf-net的更多相关文章

  1. 最快的序列化组件protobuf的.net版本protobuf.net

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

  2. Zookeeper 源码(二)序列化组件 Jute

    Zookeeper 源码(二)序列化组件 Jute 一.序列化组件 Jute 对于一个网络通信,首先需要解决的就是对数据的序列化和反序列化处理,在 ZooKeeper 中,使用了Jute 这一序列化组 ...

  3. 一种高效的序列化方式——MessagePack

    最近在弄一些数据分析方面的内容,发现很多时候数据瓶颈在模块之间的数据序列化和反序列化上了,原来项目中用的是Json,找了一圈发现Json.net在Json序列化库中已经是性能的佼佼者了,便准备从序列化 ...

  4. MessasgePack:一个小巧高效的序列化方式

    MessagePack是一种高效二进制序列化格式.可以在多种语言中进行快速数据交换,比如JSON格式等.它比Json更加小巧,更加高效,可以用于一些结构化数据存储 ,非常适合适用于消息总线,Memor ...

  5. DRF(2) - 解析器,序列化组件使用(GET/POST接口设计)

    一.DRF - 解析器 1.解析器的引出 我们知道,浏览器可以向django服务器发送json格式的数据,此时,django不会帮我们进行解析,只是将发送的原数据保存在request.body中,只有 ...

  6. DRF之解析器组件及序列化组件

    知识点复习回顾一:三元运算 三元运算能够简化我们的代码,  请看如下代码: # 定义两个变量 a = 1 b = 2 # 判断a的真假值,如果为True,则将判断表达式的前面的值赋给c,否则将判断表达 ...

  7. Restful 2 --DRF解析器,序列化组件使用(GET/POST接口设计)

    一.DRF - 解析器 1.解析器的引出 我们知道,浏览器可以向django服务器发送json格式的数据,此时,django不会帮我们进行解析,只是将发送的原数据保存在request.body中,只有 ...

  8. 解析器组件和序列化组件(GET / POST 接口设计)

    前言 我们知道,Django无法处理 application/json 协议请求的数据,即,如果用户通application/json协议发送请求数据到达Django服务器,我们通过request.P ...

  9. 分页器,序列化组件,bulk_create,choices字段

    分页器 <!--前端--> {% for book in page_queryset %} <p>{{ book.title }}</p> {% endfor %} ...

随机推荐

  1. C++创建一个名为Ellipse的椭圆类--练习

    题目描述: /*设计名为Ellipse的椭圆类*/ /* 其属性为外接矩形的左上角与右下角两个点的坐标,并能计算出椭圆的面积,并测试该类. */ 代码如下: #include<iostream& ...

  2. ABAP术语-Business Object

    Business Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/01/07/1028364.html Represents a ce ...

  3. org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field xxx exceeds its maximum permitted size of 1048576 bytes.

    springboot 通过MultipartFile接受前端传过来的文件时是有文件大小限制的(springboot内置tomact的的文件传输默认为1MB),我们可以通过配置改变它的大小限制 首先在启 ...

  4. 前端之Vue.js库的使用

    vue.js简介 Vue.js读音 /vjuː/, 类似于 view Vue.js是前端三大新框架:Angular.js.React.js.Vue.js之一,Vue.js目前的使用和关注程度在三大框架 ...

  5. Solr6.6.0添加IK中文分词器

    IK分词器就是一款中国人开发的,扩展性很好的中文分词器,它支持扩展词库,可以自己定制分词项,这对中文分词无疑是友好的. jar包下载链接:http://pan.baidu.com/s/1o85I15o ...

  6. mysql-介绍

    1.mysql几个重要的文件 每个数据库新建后,会产生数据库文件夹,在该文件夹下每张表均对应以下三个文件: xx.frm  存放表结构 xx.MYD    存放表数据 xx.MYI 存放表索引 mys ...

  7. PC时代 常用搜索引擎高级指令 勿忘

    PC时代,高级指令辅助检索,高效输出既定的需求,被广泛运用于Search Engine. 布局search入口的平台,高级指令都不可或缺.现今,高级指令的高效性,仍然主要体现在搜索引擎检索过程中. i ...

  8. Git----将本地代码推送到远程仓库

    1.初始化本地 git init 2.添加文件 -A等于 -. 和-a的集合 git add -A 3.提交 git commit -m 'add' 4.关联到远程库 git remote add o ...

  9. 学习python第一天 pycharm设置

    print(“hello,world”) pycharm设置 1. 选择python 解析器,目的是确定pycharm 的运行环境. 方法: File-->Settings-->Proje ...

  10. PAT-A1002

    1002 A+B for Polynomials (25) Polynomials多项式,exponents指数,coefficients系数 输入:两行数据,每行表示一个多项式:第一个数字表示非零项 ...