学习笔记之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

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

How to use datetime in protobuf ?

How to use Oneof ?

  • Language Guide (proto3)  |  Protocol Buffers  |  Google Developers
  • To define a oneof in your .proto you use the oneof 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 and repeated 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 of other 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 and Message.Builder interfaces shared by all Java messages and builders. For more information, see the complete API documentation for Message.
    • 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 an OutputStream.
        • static Person parseFrom(InputStream input);: reads and parses a message from an InputStream.
      • These are just a couple of the options provided for parsing and serialization. Again, see the Message API reference for a complete list.
  • 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 as str(message) or print 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 for Message.
    • 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 the str 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.
  • ByteString (google.com)
posted on 2020-07-20 14:32  浩然119  阅读(215)  评论(0编辑  收藏  举报