cyhone
cyhone

Reputation: 293

Why protobuf/python do base64 encode for bytes field in MessageToDict function

when I use json_format.MessageToDict to convert the protobuf message to python dict. the bytes type field will become the base64 encoding.

I find that source code: https://chromium.googlesource.com/external/github.com/google/protobuf/+/HEAD/python/google/protobuf/json_format.py#289

But why protobuf do that?

Upvotes: 2

Views: 3630

Answers (1)

Mond Wan
Mond Wan

Reputation: 1912

json cannot keep data in bytes. In order to put bytes inside json, you need something to encode the bytes. base64 is a common method for doing that.

As named, json_format.MessageToDict, it converts bytes into base64 encoded string to you.

Similar question here

Upvotes: 3

Related Questions