学习笔记之Protobuf
Protocol Buffers - Wikipedia
- https://en.wikipedia.org/wiki/Protocol_Buffers
- Protocol Buffers (Protobuf) is a method of serializing structured data. It is useful in developing programs to communicate with each other over a wire or for storing data. The method involves an interface description language that describes the structure of some data and a program that generates source code from that description for generating or parsing a stream of bytes that represents the structured data.
GitHub - protocolbuffers/protobuf: Protocol Buffers - Google's data interchange format
Protocol Buffers | Google Developers
- https://developers.google.com/protocol-buffers/
- Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.
- Language Guide (proto3) | Protocol Buffers | Google Developers
- https://developers.google.com/protocol-buffers/docs/proto3
- Language Guide (proto3) | Protocol Buffers | Google Developers
protoc --proto_path=IMPORT_PATH1 --proto_path=IMPORT_PATH2 --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
protobuf · PyPI
- https://pypi.org/project/protobuf/#description
- Protocol Buffers are Google’s data interchange format
图文分析:如何利用Google的protobuf,来思考、设计、实现自己的RPC框架 (qq.com)
protocol buffers - How to define an optional field in protobuf 3 - Stack Overflow
- https://stackoverflow.com/questions/42622015/how-to-define-an-optional-field-in-protobuf-3/42634681
- In proto3, all fields are "optional" (in that it is not an error if the sender fails to set them). But, fields are no longer "nullable", in that there's no way to tell the difference between a field being explicitly set to its default value vs. not having been set at all.
How to use datetime in protobuf ?
- Use the epoch time instead
How to use Oneof ?
- Language Guide (proto3) | Protocol Buffers | Google Developers
- To define a oneof in your
.proto
you use theoneof
keyword followed by your oneof name - You then add your oneof fields to the oneof definition. You can add fields of any type, except
map
fields andrepeated
fields. - In your generated code, oneof fields have the same getters and setters as regular fields. You also get a special method for checking which value (if any) in the oneof is set. You can find out more about the oneof API for your chosen language in the relevant API reference.
How to convert message to string ?
- Protocol Buffer Basics: Java | Protocol Buffers | Google Developers
- Standard Message Methods
- Each message and builder class also contains a number of other methods that let you check or manipulate the entire message, including:
isInitialized()
: checks if all the required fields have been set.toString()
: returns a human-readable representation of the message, particularly useful for debugging.mergeFrom(Message other)
: (builder only) merges the contents ofother
into this message, overwriting singular scalar fields, merging composite fields, and concatenating repeated fields.clear()
: (builder only) clears all the fields back to the empty state.
- These methods implement the
Message
andMessage.Builder
interfaces shared by all Java messages and builders. For more information, see the complete API documentation forMessage
.
- Each message and builder class also contains a number of other methods that let you check or manipulate the entire message, including:
- Parsing and Serialization
- Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:
byte[] toByteArray();
: serializes the message and returns a byte array containing its raw bytes.static Person parseFrom(byte[] data);
: parses a message from the given byte array.void writeTo(OutputStream output);
: serializes the message and writes it to anOutputStream
.static Person parseFrom(InputStream input);
: reads and parses a message from anInputStream
.
- These are just a couple of the options provided for parsing and serialization. Again, see the
Message
API reference for a complete list.
- Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:
- Standard Message Methods
- Protocol Buffer Basics: Python | Protocol Buffers | Google Developers
- Standard Message Methods
- Each message class also contains a number of other methods that let you check or manipulate the entire message, including:
IsInitialized()
: checks if all the required fields have been set.__str__()
: returns a human-readable representation of the message, particularly useful for debugging. (Usually invoked asstr(message)
orprint message
.)CopyFrom(other_msg)
: overwrites the message with the given message's values.Clear()
: clears all the elements back to the empty state.
- These methods implement the
Message
interface. For more information, see the complete API documentation forMessage
.
- Each message class also contains a number of other methods that let you check or manipulate the entire message, including:
- Parsing and Serialization
- Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:
SerializeToString()
: serializes the message and returns it as a string. Note that the bytes are binary, not text; we only use thestr
type as a convenient container.ParseFromString(data)
: parses a message from the given string.
- These are just a couple of the options provided for parsing and serialization. Again, see the
Message
API reference for a complete list.
- Finally, each protocol buffer class has methods for writing and reading messages of your chosen type using the protocol buffer binary format. These include:
- Standard Message Methods
- ByteString (google.com)