brpc
brpc copied to clipboard
json2pb::ProtoMessageToJson 将pb转为json,当遇到oneof时出错
如下代码,使用json2pb将pb转为json,当遇到oneof时输出json与protobuf不一致
#include <json2pb/pb_to_json.h>
#include <google/protobuf/util/json_util.h>
#include "test.pb.h"
/*
* test.proto
*
syntax = "proto3";
message Test{
oneof OneOf{
string a = 1;
string b = 2;
string c = 3;
}
}
*/
int main()
{
Test test;
test.set_a("aaa");
{
std::string strJson;
json2pb::Pb2JsonOptions options;
options.always_print_primitive_fields = true;
json2pb::ProtoMessageToJson(test, &strJson, options);
std::cout << "json2pb: " << strJson << std::endl;
}
{
std::string strJson;
google::protobuf::util::JsonOptions options;
options.always_print_primitive_fields = true;
google::protobuf::util::MessageToJsonString(test, &strJson, options);
std::cout << "google: " << strJson << std::endl;
}
return 0;
}
以上代码中输出为:
json2pb: {"a":"aaa","b":"","c":""}
google: {"a":"aaa"}
brpc版本:1.12.1