Get default values when serializing data in Python
21 views
Skip to first unread message
Mathias Hjärtström
unread,
Dec 2, 2024, 7:01:02 AM12/2/24
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nanopb
Hello,
I have a protobuf schema using default values for the defined nanopb messages, but when I try to use the Python version of the generated code to serialize the data I have to explicitly set the value of each field otherwise I'll get an EncodeError exception telling me "missing required fields".
How to I make sure the default values are initialized?
How do I make it only serialize values that differ from the default values, if possible?
BR,
Mathias
Petteri Aimonen
unread,
Dec 3, 2024, 1:46:41 AM12/3/24
Reply to author
Sign in to reply to author
Forward
Sign in to forward
Delete
You do not have permission to delete messages in this group
Copy link
Report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to nan...@googlegroups.com
Hi,
I assume this is proto2 syntax and the fields in question are specified
as "required".
Normally in protobuf, the default value is handled on the receiving side,
and the fields would be marked optional. This way when the field value
is not changed, it is not included in the encoded message and it is smaller.
The receiving end then fills in the default value from its proto definition.
It is somewhat funny that the protobuf format allows specifying default
values for required fields. I don't see much use in this.