1

I expect that there is no missing value when i serialize and parse, but protoc version 3.9.2 have a problem.

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"
    }
  ]
}

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, but if i really want to get the default value, i need to get the value of every params, what should i do? i have modify the has_default_value, default_value, but it doesn't work
Thank you again ,i solve my problem!!! i can parse the pbstring by setting including_default_value_fields=True, and then dealing the data..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.