Enum field encoding question

27 views
Skip to first unread message

Burak Kirazli

unread,
Jan 16, 2020, 3:08:39 AM1/16/20
to Protocol Buffers
Hello,

I am new to protobuf. 

I have a request message like that

message CommandRequest
{
    enum Code
    {
        RESET = 0;
        ERASE_APP = 2;
        ERASE_OTA = 3;
    }
    
    Code code = 1;
}

When I am trying to encode a request with zero value, i see that encoded size is zero. What will be exchanged?

    //Qt project

    CommandRequest req;
    req.set_code(CommandRequest_Code::CommandRequest_Code_RESET);
    qDebug() << req.ByteSizeLong() << endl;
    
    //Output of this code is:
    0


Thanks.

Marc Gravell

unread,
Jan 16, 2020, 3:12:06 AM1/16/20
to Burak Kirazli, Protocol Buffers
Hi; default values *are not sent*, especially in proto3 where zero is default and default is zero. Likewise, the root object in a message is not wrapped in any way - only fields *on* the root object.

This means that the binary encoding of a CommandRequest with Code.RESET is: zero bytes, which is a perfectly valid size in protobuf.

Sometimes, people find it convenient / helpful to use a DEFAULT / NONE / UNKNOWN = 0, with their *actual* enum values above that. This makes the default experience easier to understand, and avoid problems.

Marc

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/376341b9-b429-4abe-b9ea-aeb2834c6f58%40googlegroups.com.


--
Regards,

Marc
Message has been deleted

Alexey Edelev

unread,
Feb 26, 2020, 10:29:40 AM2/26/20
to Protocol Buffers
Hi,

I think this project might be useful for you https://github.com/semlanik/qtprotobuf

четверг, 16 января 2020 г., 9:08:39 UTC+1 пользователь Burak Kirazli написал:
Reply all
Reply to author
Forward
0 new messages