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
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
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.

--
Petteri
> --
> You received this message because you are subscribed to the Google Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to nanopb+un...@googlegroups.com.
> To view this discussion visit https://groups.google.com/d/msgid/nanopb/a41f504b-76d1-4c4c-ba83-0658b20ca139n%40googlegroups.com.
> --
> You received this message because you are subscribed to the Google
> Groups "nanopb" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to [1]nanopb+un...@googlegroups.com.
> To view this discussion visit
> [2]https://groups.google.com/d/msgid/nanopb/a41f504b-76d1-4c4c-ba83-065
> 8b20ca139n%40googlegroups.com.
>
> References
>
> 1. mailto:nanopb+un...@googlegroups.com
> 2. https://groups.google.com/d/msgid/nanopb/a41f504b-76d1-4c4c...@googlegroups.com?utm_medium=email&utm_source=footer


Reply all
Reply to author
Forward
0 new messages