Silpa KS
Silpa KS

Reputation: 31

Python API to parse binary file which contains protobuf data in array format

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?

Upvotes: 3

Views: 1905

Answers (1)

jpa
jpa

Reputation: 12176

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.

Upvotes: 1

Related Questions