Parsing Protocol Buffers, written in Java and read in Python

断了今生、忘了曾经 提交于 2020-01-04 05:12:09

问题


I am dealing with a situation where the protocol buffers have been written to in Java, but I have to read them using Python.

I have the .proto file with me, I have compiled it using protoc, and generated the python class file.

The proto file has multiple messages, for example:

message ABC {
    optional int64 id = 1 [default = -1];
    optional int64 hello6 = 2;
    optional bool hello5 = 3 [default = true];
    optional int32 hello4 = 4;
    optional int64 hello3 = 5;
    optional int64 hello2 = 6;
    optional int64 duration = 7;
}
message DEF {
    optional int64 hello = 1;
    repeated ABC abc = 5;
    repeated String xyz = 6;
  }

While parsing in python using .ParseFromString.

I am getting the error:

google.protobuf.message.DecodeError: Tag had invalid wire type.

I think the issue is because of absence of explicit delimiters, any idea how to solve it?


回答1:


I had similar issue for me this worked:

In Java you do:

yourGeneratedClass.toByteArray()

In Python you do(parsed message in result):

result = your_pb2.yourGeneratedClass()
result.ParseFromString(body)


来源:https://stackoverflow.com/questions/34507714/parsing-protocol-buffers-written-in-java-and-read-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!