Why is my Protobuf message (in Python) ignore zero value?

I have been working on implementing IPC’s protobufs for the project. For some reason, the value of 0 is not set/serialized. For the context, the .proto file contains the following message:

syntax = "proto3";

enum SetGet {
SET = 0;
GET = 1;
}

message State {
SetGet setget = 1;
double x = 2;
double y = 3;
double depth = 4;
double yaw = 5;
double pitch = 6;
double roll = 7;
}

I use protoc to compile the file into a Python _pb2 file, Then try to run the following test script:

import filename_pb2 as pb

state = pb.State()
state.x = 0< br />state.y = 0
state.depth = 0
state.yaw = 0
state.pitch = 0
state.roll = 0
state. setget = pb.SET

print("State: {}".format(state))

state2 = pb.State()
state2.ParseFromString( state.SerializeToString())

print("State2: {}".format(state2))

When I run it, the following output is printed:

< p>

State: 
State2:

Nothing seems to be set, or the zero value is somehow ignored.
However, when I set the value ( x, y, depth, etc.) is changed to a non-zero value (e.g. 0.1), I The following expected results are obtained:

State: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch : 0.1
roll: 0.1

State2: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch: 0.1
roll: 0.1

Even if the number is printed out, for some reason, the enumeration is still not.
Why does this happen on protobufs? The default is double type 0, so will the protobuf serializer ignore them to save space? So, why are they not restored when parsing State2? Did I miss something in the document? Thanks in advance!

– Tim

Yes, 0 is the default value. In the documentation This situation is clearly mentioned in:

Note that for scalar message fields, once a message is parsed there’s no way of telling whether a field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types. For example, don’t have a boolean that switches on some behaviour when set to false if you don’t want that behaviour to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.

< /div>

I have been working on implementing IPC’s protobufs for the project. For some reason, the value of 0 is not set/serialized. For the context, the .proto file contains the following message:

syntax = "proto3";

enum SetGet {
SET = 0;
GET = 1;
}

message State {
SetGet setget = 1;
double x = 2;
double y = 3 ;
double depth = 4;
double yaw = 5;
double pitch = 6;
double roll = 7;
}

I Use protoc to compile the file into a Python _pb2 file, and then try to run the following test script:

import filename_pb2 as pb

state = pb.State()
state.x = 0
state.y = 0
state.depth = 0
state.yaw = 0
state.pitch = 0
state .roll = 0
state.setget = pb.SET

print("State: {}".format(state))

state2 = pb.State ()
state2.ParseFromString(state.SerializeToString())

print("State2: {}".format(state2))

When I run it , Print the following output:

State: 
State2:

Nothing seems to be set, or the zero value is somehow ignored.
However, when I change the value (x, y, depth, etc.) to a non-zero value (e.g. 0.1), I get the following expected result:

State: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch: 0.1
roll: 0.1

State2: x: 0.1
y: 0.1
depth: 0.1
yaw: 0.1
pitch: 0.1
roll: 0.1

Even if the number is printed, because For some reason, enumeration is still not.
Why does this happen on protobufs? The default is double type 0, so will the protobuf serializer ignore them to save space? So, why are they not restored when parsing State2? Did I miss something in the document? Thanks in advance!

– Tim

Yes, 0 is the default value. This situation is clearly mentioned in the documentation:

< /p>

Note that for scalar message fields, once a message is parsed there’s no way of telling whether a field was explicitly set to the default value (for example whether a boolean was set to false) or just not set at all: you should bear this in mind when defining your message types. For example, don’t have a boolean that switches on some behaviour when set to false if you don’t want that behaviour to also happen by default. Also note that if a scalar message field is set to its default, the value will not be serialized on the wire.

Leave a Comment

Your email address will not be published.