Protobuf doesn't serialize default values
Asked Answered
E

3

9

I'm using Protobuf for python.

I've been trying to use default values but everytime I run SerializeToString() i get nothing.

For example,

here is my .proto file object

message Test{

    optional string lol = 1 [default="HI"];
    optional int32 num = 2 [default=200];
}

I run

test = packets_pb2.Test()
print(test.num)
print(test.SerializeToString())

and get 200 for print(test.num) but no results (empty) for SerializeToString()

I want my default values to be serialized.

Any idea how to get this done?

Thanks in advance.

Ethanol answered 24/6, 2015 at 8:43 Comment(0)
O
8

This is working as intended. Default values are not sent on the wire. Instead, the receiving end assumes that if a field isn't present, then it should use the default value. This saves space on the wire by not sending common values. This does mean that the client and server have to agree on the default values; you generally should not change the default values in your .proto files.

Keep in mind that the main purpose of default values is to be able to handle messages from old clients that were built before the field existed. So, those clients clearly can't send the default value on the wire since they don't know anything about it.

Otic answered 25/6, 2015 at 19:13 Comment(0)
K
13

For anyone using Protobuf 3, there is a way to serialize the default values using the including_default_value_fields argument of MessageToDict or MessageToJson:

from google.protobuf.json_format import MessageToJson

serialized_message_with_defaults = MessageToJson(
    protobuf_instance,
    including_default_value_fields=True,  # this does it
)
Katabasis answered 9/5, 2019 at 18:51 Comment(1)
Is there any way to do it on proto2 ?Diagenesis
O
8

This is working as intended. Default values are not sent on the wire. Instead, the receiving end assumes that if a field isn't present, then it should use the default value. This saves space on the wire by not sending common values. This does mean that the client and server have to agree on the default values; you generally should not change the default values in your .proto files.

Keep in mind that the main purpose of default values is to be able to handle messages from old clients that were built before the field existed. So, those clients clearly can't send the default value on the wire since they don't know anything about it.

Otic answered 25/6, 2015 at 19:13 Comment(0)
R
0

Franey's answer is correct, however with protobuf==5.26.1 instead of including_default_value_fields=True use always_print_fields_with_no_presence=True

Reyreyes answered 19/6 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.