我正在为 python 使用 Protobuf。
我一直在尝试使用默认值,但每次运行 SerializeToString() 时我什么也得不到。
例如,
这是我的 .proto 文件对象
message Test{
optional string lol = 1 [default="HI"];
optional int32 num = 2 [default=200];
}
I run
test = packets_pb2.Test()
print(test.num)
print(test.SerializeToString())
并得到
200 用于打印(test.num)
但 SerializeToString() 没有结果(空)
我希望我的默认值被序列化。
知道如何完成这件事吗?
提前致谢。
对于任何使用 Protobuf 3 的人来说,有一种方法可以使用以下方法序列化默认值:including_default_value_fields
的论证MessageToDict
or MessageToJson:
from google.protobuf.json_format import MessageToJson
serialized_message_with_defaults = MessageToJson(
protobuf_instance,
including_default_value_fields=True, # this does it
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)