Jeff Wang - @jf423
Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install automake
brew install libtool
brew install protobuf
The definition of protobuf to serialize/deserialize the data.
(.proto)
syntax = "proto3";
package Ecommerce;
message Product {
bool available = 1;
string name = 2;
string desc = 3;
float price = 4;
repeated Item item = 5;
}
message Item {
string title = 1;
int32 number = 2;
}
[-] .proto
syntax = "proto3";
package Ecommerce;
message Product {
bool available = 1;
string name = 2;
string desc = 3;
float price = 4;
repeated Item item = 5;
}
message Item {
string title = 1;
int32 number = 2;
}
version type
message collection
field's number
field's name
field's type
message type (object)
protoc -I=$SRC_DIR --$FORMAT=$DST_DIR $SRC_DIR/xxx.proto
/*
$SRC_DIR=source directory,
$DST_DIR=target directory,
$FORMAT=output language (cpp_out, java_out...)
*/
The static interface of protobuf implementation.
syntax = "proto3";
package Ecommerce;
message Product {
bool available = 1;
string name = 2;
string desc = 3;
float price = 4;
repeated Item item = 5;
}
message Item {
string title = 1;
int32 number = 2;
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: test.proto
#include "test.pb.h"
#include <algorithm>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/port.h>
#include ...
// @@protoc_insertion_point(includes)
namespace Ecommerce {
class ProductDefaultTypeInternal {
public:
::google::protobuf::internal::...
_instance;
} _Product_default_instance_;
...
// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: test.proto
#ifndef PROTOBUF_test_2eproto__INCLUDED
#define PROTOBUF_test_2eproto__INCLUDED
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/io/coded_stream.h>
#include ...
// @@protoc_insertion_point(includes)
namespace Ecommerce {
class Item;
class ...
} // namespace Ecommerce
...
test.proto
test.pb.cc
test.pb.h
👿
😇
pure JavaScript implementation with TypeScript support for node.js and the browser.
better performance, better documentation
npm install -g protobufjs
npm install protobufjs [--save --save-prefix=~]
var protobuf = require("protobufjs");
// Development
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.js"></script>
// Production
<script src="//cdn.rawgit.com/dcodeIO/protobuf.js/6.X.X/dist/protobuf.min.js"></script>
# ComoonJS
pbjs -t static-module -w commonjs -o $DST_DIR/xxx-common.js $SRC_DIR/xxx.proto
# ES6
pbjs -t static-module -w es6 -o $DST_DIR/xxx-es6.js $SRC_DIR/xxx.proto
# json
pbjs -t json -o $DST_DIR/xxx.json $SRC_DIR/xxx.proto
# json-module
pbjs -t json-module -o $DST_DIR/xxx-json.js $SRC_DIR/xxx.proto
/*
$SRC_DIR=source directory,
$DST_DIR=target directory,
$FORMAT=output language (cpp_out, java_out...)
*/
import ProtoBuf from 'protobufjs';
import proto from './schema/protos/xxx.proto';
ProtoBuf.load(proto, function(err, root) {
if (err)
throw err;
const Product = root.lookup('Ecommerce.Product');
const result = Product.decode(data);
console.log(result);
});
import { Ecommerce } from './interface/test-es6';
const result = Ecommerce.Product.decode(data);
console.log(result);
Source | Library | Advantages | Tradeoffs |
---|---|---|---|
.proto (Reflection) |
full | Easily editable Interoperability with other libraries No compile step |
Some parsing and possibly network overhead |
JSON (Reflection) |
light | Easily editable No parsing overhead Single bundle (no network overhead) |
protobuf.js specific Has a compile step |
static (Static code) |
minimal | Works where eval access is restricted Fully documented Small footprint for small protos |
Can be hard to edit No reflection Has a compile step |
interface
message
encode
decode
binary
.proto
interface
message
Documentation generator plugin for the Protocol Buffers.
docker pull pseudomuto/protoc-gen-doc
docker run --rm \
-v $DST_DIR:/out \
-v $SRC_DIR:/protos \
pseudomuto/protoc-gen-doc --doc_opt=$FORMAT,$FILE
/*
$SRC_DIR=source directory,
$DST_DIR=target directory,
$FORMAT=output form (Markdown, HTML, JSON, DocBook)
$FILE=the output file (xxx.md, xxx.html...)
*/
A central repository for all proto files of PokémonGO.
syntax = "proto3";
package POGOProtos.Map.Pokemon;
import "POGOProtos/Enums/PokemonId.proto";
import "POGOProtos/Data/PokemonDisplay.proto";
message NearbyPokemon {
.POGOProtos.Enums.PokemonId pokemon_id = 1;
float distance_in_meters = 2;
fixed64 encounter_id = 3;
string fort_id = 4;
string fort_image_url = 5;
.POGOProtos.Data.PokemonDisplay pokemon_display = 6;
}
an open source remote procedure call (RPC) system
https://github.com/google/protobuf/tree/master/js
https://github.com/dcodeIO/ProtoBuf.js/
https://github.com/pseudomuto/protoc-gen-doc