Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.
谷歌出的一种语言无关、平台无关、可扩展的序列化结构数据的方法,它可用于(数据)通信协议、数据存储等。灵活,高效,自动化机制的结构数据序列化方法-可类比 XML,但是比 XML 更小(3 ~ 10倍)、更快(20 ~ 100倍)、更为简单。比JSON也小,最简单的理解就是健可以不用重复,通过定义数据的结构,然后使用特殊生成的源代码轻松的在各种数据流中使用各种语言进行编写和读取结构数据。甚至可以更新数据结构,而不破坏由旧数据结构编译的已部署程序。
.proto
file. / 定义数据格式1. Define data format (t1d.proto):
syntax = "proto3";
message Device {
string name = 1;
}
2. Compile file:
https://developers.google.com/protocol-buffers/docs/downloads
https://github.com/protocolbuffers/protobuf
# NETWORK issue -> proxy
pip install protobuf
brew install protobuf
protoc --version
# make (option in mac)
./autogen.sh && ./configure && make
brew install autoconf && brew install automake
make check
sudo make install
which protoc
# Compile now
# protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/t1d.proto
# protoc --proto_path=./ --python_out=./ my_example.proto
protoc -I=./ --python_out=./data/ t1d.proto
3. Runtime write and read:
import t1d_pb2
my_device = t1d_pb2.Device(name='IHU!')
with open('my_device.pb', 'wb') as f:
f.write(my_device.SerializeToString())
with open('my_device.pb', 'rb') as f:
new_device = t1d_pb2.Device().FromString(f.read())
protoc --js_out=import_style=commonjs,binary:. XXX.proto
npm install -g require
npm install -g browserify
npm install google-protobuf
var address = require('./address_pb');
module.exports = {
DataProto: address
}
browserify exports.js -o address_main.js
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。