3

I'm facing problem in parsing binary file containing protobuf data in array format.

When I used the python API, ParseFromString output was always zero or none.

The serialized protobuf data in the binary file was generated using C++ API, SerializeToArray. But to parse the bin file, I want to use python for parsing. But in python I couldn't find any API to parse serialized protobuf data as array in binary file.

Is there any python API to resolve this issue? Can anyone please suggest a solution for this?

Keren Caelen
  • 1,466
  • 3
  • 17
  • 38
Silpa KS
  • 31
  • 3

1 Answers1

1

The C++ interface:

bool SerializeToArray(void * data, int size) const

just stores the binary data into a byte array, instead of a string object. If it is then written to file, it can be deserialized in the normal way, as shown in the tutorial:

address_book = addressbook_pb2.AddressBook()
f = open(sys.argv[1], "rb")
address_book.ParseFromString(f.read())

The fact that SerializeToArray() was used on C++ side does not change the format of the file. Of course the C++ code could e.g. write a custom header to the file that needs to be removed, but that would be a different part of the code.

jpa
  • 10,351
  • 1
  • 28
  • 45