Reputation: 13
the protofile is show below:
syntax = "proto3";
package example;
message person {
int32 id = 1;
string name = 2;
}
message all_person {
repeated person Per = 1;
}
when i set id=0, name='hello' , i expect to show the "id": 0, but after SerializeToString and parseToString, it returns
{
"Per": [
{
"name": "hello"
}
]
}
but if i set id=1,name='hello', it returns
{
"Per": [
{
"id": 1,
"name": "hello"
}
]
}
Upvotes: 1
Views: 1253
Reputation: 881113
Zero is the default value for numerics (similarly, strings default to empty, booleans to false). See here for more details.
For efficiency, Protobuf relies on these default value. In our system (using FastRTPS and Protobuf for pub/sub), default values are not transmitted across the wire. Based on what you're seeing, it doesn't worry about them for serialisation either.
However, this is only the default behaviour and may be changeable. For example, if you're using MessageToJson
, you can simply set an optional parameter including_default_value_fields
to True
, stating you want the defaults output as well:
jsonStr = MessageToJson(myMsg, True)
Upvotes: 3