Google Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前支持主流的语言(java\c++\python\c#\go....)。

具官方说法使用PB 进行序列化和反序列化性能是xml序列化和反序列化的20~100倍,在各种序列化和反序列化方式中,处于领先地位。

PB的序列化结果是字节与字节的连接,省略了字段名称,在存储上也比其他方式要小,更节省空间。不过这样带来了可读性变差和调试困难。

官方源码:https://github.com/google/protobuf

各版本的编译结果下载地址:https://github.com/google/protobuf/releases

官方介绍:https://developers.google.com/protocol-buffers/

1、下载protoc

在https://github.com/google/protobuf/releases中,可以看到多个系统的protoc文件:

protoc-3.3.0-linux-x86_32.zip

protoc-3.3.0-linux-x86_64.zip

protoc-3.3.0-osx-x86_32.zip

protoc-3.3.0-osx-x86_64.zip

protoc-3.3.0-win32.zip

下载后,解压就可以。

2、在解压目录创建一个*.proto 文件。

如:test.proto

syntax = "proto3";

message test{

double Id=1;

string Name=2;

}

syntax = "proto3"; 为指定一个版本,此含义为使用proto3

message为关键字,test为类名

double/string为类字段类型,Id/Name为字段属性,1/2为存储序号,定义后不可变

详细定义和使用方法参考:https://developers.google.com/protocol-buffers/docs/proto3#simple

3、生成各种语言的类文件

protoc --cpp_out="./"  --java_out="./"   --csharp_out="./"  ./test.proto

“./”为当前protoc所在目录,protoc为可执行文件

4、序列化与反序列化(以下C#示例)

 Google.Protobuf.IMessage test= new test() {  Id= 3333555555234234.333, Name= "dfsdfsdfsdfsdfsdfsdfs"};  //创建test对象

 byte[] buf = null;
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
//序列化
using (Google.Protobuf.CodedOutputStream output = new Google.Protobuf.CodedOutputStream(ms))
{
test.WriteTo(output);
} buf = ms.ToArray();
} test newTest = new test();
using (Google.Protobuf.CodedInputStream input = new Google.Protobuf.CodedInputStream(buf))
{
//反序列化
newTest.MergeFrom(input);
}

Google Protocol Buffers 入门的更多相关文章

  1. Google Protocol Buffers 快速入门(带生成C#源码的方法)

    Google Protocol Buffers是google出品的一个协议生成工具,特点就是跨平台,效率高,速度快,对我们自己的程序定义和使用私有协议很有帮助. Protocol Buffers入门: ...

  2. Google Protocol Buffers介绍

    简要介绍和总结protobuf的一些关键点,从我之前做的ppt里摘录而成,希望能节省protobuf初学者的入门时间.这是一个简单的Demo. Protobuf 简介 Protobuf全称Google ...

  3. C# 使用Google Protocol Buffers

    Google Protocol Buffers 使用3.0版本 下载protoc.exe 下载链接 https://github.com/protocolbuffers/protobuf/releas ...

  4. Google Protocol Buffers简介

    什么是 protocol buffers ? Protocol buffers 是一种灵活.高效的序列化结构数据的自动机制--想想XML,但是它更小,更快,更简单.你只需要把你需要怎样结构化你的数据定 ...

  5. C#/net 使用Protocol Buffers入门

    Protocol buffers 是一个由谷歌开发的开源的编码机制用于将结构化的数据序列化或者反序列化,被设计成语言以及平台中立,protobuff比xml更简单比json还要紧凑一些,网上有一些关于 ...

  6. Java使用Protocol Buffers入门四步骤

    Protocol Buffers(简称protobuf)是谷歌的一项技术.用于将结构化的数据序列化.反序列化.经经常使用于网络传输. 这货实际上相似于XML生成和解析.但protobuf的效率高于XM ...

  7. Google Protocol Buffer入门

    简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...

  8. Google Protocol Buffers 反序列化 转

    http://www.cnblogs.com/royenhome/archive/2010/10/30/1865256.html   本文作为结束篇,会稍微介绍下怎么反序列化GoogleBuffer数 ...

  9. Google Protocol Buffers和java字符串处理控制

    大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...

随机推荐

  1. 范围for循环(c++11)

    1.概念 1)c++11新标准下用范围for循环来遍历序列 2)使用范围for循环时,如果要修改序列中的元素,则必须把循环变量定义成引用类型: int main() { string s = &quo ...

  2. C++ cout

    cout.flush() cout.put() 输出一个字符 char* p = "hello"; cout.write(p,sttrlen(q)-3) 输出字符串,能够选定长度. ...

  3. IntelliJ IDEA 2017版 Spring5 的RunnableFactoryBean配置

    1.新建RunnableFactoryBean package com.spring4.pojo; import org.springframework.beans.factory.FactoryBe ...

  4. php,微信公众号,获取用户地理位置 定位 经纬度

    <?php //php插件下载地址: https://files.cnblogs.com/files/fan-bk/jssdk_php.rar //建立一个php文件 require_once ...

  5. Topcoder-SRM-#712-Div2

    250-RepeatNumberCompare Problem Statement For any two positive integers x and k we can make a new nu ...

  6. 如何比较两个xml 的异同

    http://www.xmlunit.org/ <dependency>     <groupId>org.xmlunit</groupId>     <ar ...

  7. TinyMCE Editor

    TinyMCE Editor(https://www.tinymce.com/features/) is an online text editor, it is used to write post ...

  8. Java学习介绍

    Java版本介绍 JavaME:微型版,用于开发小型设备.智能卡.移动终端应用(使用率较低) JavaSE:标准版,用于创建桌面应用(企业用JavaSE创建桌面应用较少) JavaEE:企业版,用于创 ...

  9. 关于DFS和BFS的理解 以及坐标的定义

    http://blog.csdn.net/bool_isprime/article/details/5803018DFS: 1: 坐标类型搜索 :这种类型的搜索题目通常来说简单的比较简单,复杂的通常在 ...

  10. MySQL表名大小写设置

    1 简介    在MySQL中,数据库对应数据目录中的目录.数据库中的每个表至少对应数据库目录中的一个文件(也可能是多个,取决于存储引擎).因此,所使用操作系统的大小写敏感性决定了数据库名和表名的大小 ...