serialization - protobuf 消息的分隔符是什么?

标签 serialization protocol-buffers

protobuf 消息的分隔符是什么?我正在处理序列化消息。我想知道消息是否以 $$__$$ 开头并以相同的符号结尾。

最佳答案

(除了现有的答案 12 )

Protocol Buffer 的常用框架方法是在前面添加 varint在实际的 protobuf 消息之前。

该实现已经是 protobuf 库的一部分,例如:

  • 对于 Java:MessageLite.writeDelimitedTo() , Parser.parseDelimitedFrom()
  • 对于 C:标题中的方法 google/protobuf/util/delimited_message_util.h (例如 SerializeDelimitedToFileDescriptor() )

  • 祝你的项目好运!

    编辑> The official reference指出:

    If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. (If you want to avoid copying bytes to a separate buffer, check out the CodedInputStream class (in both C++ and Java) which can be told to limit reads to a certain number of bytes.)

    关于serialization - protobuf 消息的分隔符是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336629/

    相关文章:

    java - 如何更新文件中包含的序列化 HashMap?

    serialization - 如何让 RestSharp 使用 BSON?

    python - Protocol Buffer导入解析

    c++ - 交叉编译Qt to Raspberry Pi 3B+ addressbook (Protocol Buffer) - error symbol lookup error

    python-3.x - 如何从 Python Dict 创建 Protobuf Struct?

    c# - Protobuf-Net 错误消息 : No Serializer defined for type: System. 类型

    compiler-construction - 如何在 IntelliJ IDEA 中为某些文件类型定义自定义编译器?

    c# - 无法序列化/反序列化传递给 WCF 方法的参数

    php - 来自 PHP 的 Python Pickle 反序列化?

    javascript - 将 serialize() 转换为对象数组以发布到 Controller