6

Protobuf:转成json不显示零值字段问题

 3 years ago
source link: https://www.lpime.cn/article/108
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

ProtobufC++

protobuf转成json不显示零值字段问题解决

前提:所用语言:C++,版本:最新

问题描述:在使用枚举值时,默认值是从0开始的,导致使用MessageToJsonString转成json字符串时,零值的字段不显示。

enum num
{
	a = 0;
	b = 1;
	c = 2;
}

解决方式:

查看源代码在json_util.h文件中,有转成json字符串选项JsonOptions。修正我们的代码如

std::string out;
google::protobuf::util::JsonOptions options;
options.always_print_primitive_fields = true;   //显示默认值
google::protobuf::util::MessageToJsonString(xxx, &out, options);

本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2021/01/04 17:03


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK