Parse from string returns false

490 views
Skip to first unread message

Krati Chordia

unread,
Jun 14, 2023, 4:37:05 AM6/14/23
to Protocol Buffers
Hi,
I am trying to send a message on wire with the following protobuf structure

message TestMsg {
    string status = 1;
}

I create an instance of TestMsg and set status as empty and serialize it to a string.

TestMsg m1;
m1.set_status("");

std::string str = m1.SerializeAsString();

Post serialization, str is sent over wire and tried to be parsed. ParseFromString returns false whereas it should not. For any other value, it parses successfully.

TestMsg m2;
m2.ParseFromString(str);  <----- this returns false

Also, if I try to retrieve the value of m2.status(), it will return an empty string even though the parsing returns false.

Deanna Garcia

unread,
Jun 15, 2023, 1:50:56 PM6/15/23
to Protocol Buffers
In protobuf, we can't store null strings so to denote a string that isn't set we use the empty string. For this reason, the parsing is returning false to tell you that there isn't a field there but as you're seeing will still parse as an empty string.

Krati Chordia

unread,
Jun 16, 2023, 8:40:59 AM6/16/23
to Protocol Buffers
Hi Deanna!
Agreed to your point. But if there is an integer and it's value is set to 0, even then the parsing returns false. Which should not happen.

Deanna Garcia

unread,
Jun 16, 2023, 12:53:58 PM6/16/23
to Protocol Buffers
It's the same situation with integers. If we made this return true then if you did not set the value and attempted to serialize it it would return true. I agree that this behavior isn't optimal, we just don't have a great way around it.

Krati Chordia

unread,
Jun 20, 2023, 12:57:12 AM6/20/23
to Protocol Buffers
Thanks much for your input. 
-Krati

Reply all
Reply to author
Forward
0 new messages