Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在python中,当protobuf等于0时,为什么会遗漏一些参数? 我希望在序列化和解析时不会丢失任何值,但protoc版本3.9.2有一个问题。_Python 3.x_Protocol Buffers - Fatal编程技术网

Python 3.x 在python中,当protobuf等于0时,为什么会遗漏一些参数? 我希望在序列化和解析时不会丢失任何值,但protoc版本3.9.2有一个问题。

Python 3.x 在python中,当protobuf等于0时,为什么会遗漏一些参数? 我希望在序列化和解析时不会丢失任何值,但protoc版本3.9.2有一个问题。,python-3.x,protocol-buffers,Python 3.x,Protocol Buffers,协议文件如下所示: syntax = "proto3"; package example; message person { int32 id = 1; string name = 2; } message all_person { repeated person Per = 1; } 当我设置id=0,name='hello'时,我希望显示“id”:0, 但是在序列化ToString和parseToString之后,它返回 { "Per": [ {

协议文件如下所示:

syntax = "proto3";
package example;
message person {
    int32 id = 1;
    string name = 2;
}
message all_person {
    repeated person Per = 1;
}
当我设置id=0,name='hello'时,我希望显示“id”:0, 但是在序列化ToString和parseToString之后,它返回


{
  "Per": [
    {
      "name": "hello"
    }
  ]
}
{
  "Per": [
    {
      "id": 1,
      "name": "hello"
    }
  ]
}
但是如果我设置id=1,name='hello',它就会返回


{
  "Per": [
    {
      "name": "hello"
    }
  ]
}
{
  "Per": [
    {
      "id": 1,
      "name": "hello"
    }
  ]
}
零是数字的默认值(类似地,字符串默认为空,布尔值默认为假)。有关更多详细信息,请参阅

为了提高效率,Protobuf依赖于这些默认值。在我们的系统中(使用FastRTPS和Protobuf进行发布/订阅),默认值不会通过网络传输。根据您所看到的,它也不担心它们的序列化

但是,这只是默认行为,可能是可变的。例如,如果您正在使用,您只需将一个可选参数
设置为
True
,说明您还需要默认输出:

jsonStr = MessageToJson(myMsg, True)

谢谢你的回答,但是如果我真的想得到默认值,我需要得到每个参数的值,我应该怎么做?我已经修改了has_default_值,default_值,但它不起作用再次感谢你,我解决了我的问题!!!我可以通过设置include_default_value_fields=True来解析pbstring,然后处理数据。。