'what is the difference between protobuf serialize in python and java?

I try to use java and python protobuf serialize method,such as

in python, the api is

SerializeToString

in java ,the api is

toByteArray

the result is the same? if not , how can i translate the java result as python result?



Solution 1:[1]

The reason for the different naming is because of conventions in these respective languages:

  • In Python 2, a string (str) was a series of bytes, hence SerializeToString. In Python 3 we have the bytes type for byte strings, but the name was kept.

  • In Java, the returned value is a byte[] so the name toByteArray makes sense.

However, despite the differences in API, the protobuf serialization format "on the wire" (or on disk) is fully compatible between languages and platforms:

The same messages can be read by code written in any supported programming language. You can have a Java program on one platform capture data from one software system, serialize it based on a .proto definition, and then extract specific values from that serialized data in a separate Python application running on another platform.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomas