|
前言
'protobuf 3.0 版本支持 protobuf 与 json 数据相互转换。
文章来源:protobuf / json 数据转换(C++) 1. 转换接口
protobuf 与 json 数据转换接口在 google/protobuf/util/json_util.h 文件里。
/* protobuf 转 json。 */
inline util::Status MessageToJsonString(const Message& message, std::string* output);
/* json 换 protobuf。 */
inline util::Status JsonStringToMessage(StringPiece input, Message* message);
2. 测试
- protobuf 文件(nodes.proto)。
syntax = "proto3";
package kim;
message addr_info {
string bind = 1; /* bind host for inner server. */
uint32 port = 2; /* port for inner server. */
string gate_bind = 3; /* bind host for user client. */
uint32 gate_port = 4; /* port for user client. */
}
message node_info {
string name = 1; /* read from config and register to zk. */
addr_info addr_info = 2; /* network addr info. */
string node_type = 3; /* node type in cluster. */
string conf_path = 4; /* config path. */
string work_path = 5; /* process work path. */
int32 worker_cnt = 6; /* number of worker's processes. */
}
- 执行脚本将 proto 文件生成 C++ protobuf 代码。
protoc -I. *.proto --cpp_out=.
- 测试代码(test_proto_json.cpp)。
#include <google/protobuf/util/json_util.h>
#include <iostream>
#include &#34;nodes.pb.h&#34;
using google::protobuf::util::JsonStringToMessage;
bool proto_to_json(const google::protobuf::Message& message, std::string& json) {
google::protobuf::util::JsonPrintOptions options;
options.add_whitespace = true;
options.always_print_primitive_fields = true;
options.preserve_proto_field_names = true;
return MessageToJsonString(message, &json, options).ok();
}
bool json_to_proto(const std::string& json, google::protobuf::Message& message) {
return JsonStringToMessage(json, &message).ok();
}
int main() {
kim::node_info node;
std::string json_string;
node.set_name(&#34;111111&#34;);
node.set_node_type(&#34;34rw343&#34;);
node.set_conf_path(&#34;reuwyruiwe&#34;);
node.set_work_path(&#34;ewiruwe&#34;);
node.set_worker_cnt(3);
node.mutable_addr_info()->set_bind(&#34;xxxxxxxxxx&#34;);
node.mutable_addr_info()->set_port(342);
node.mutable_addr_info()->set_gate_bind(&#34;fsduyruwerw&#34;);
node.mutable_addr_info()->set_gate_port(4853);
/* protobuf 转 json。 */
if (!proto_to_json(node, json_string)) {
std::cout << &#34;protobuf convert json failed!&#34; << std::endl;
return 1;
}
std::cout << &#34;protobuf convert json done!&#34; << std::endl
<< json_string << std::endl;
node.Clear();
std::cout << &#34;-----&#34; << std::endl;
/* json 转 protobuf。 */
if (!json_to_proto(json_string, node)) {
std::cout << &#34;json to protobuf failed!&#34; << std::endl;
return 1;
}
std::cout << &#34;json to protobuf done!&#34; << std::endl
<< &#34;name: &#34; << node.name() << std::endl
<< &#34;bind: &#34; << node.mutable_addr_info()->bind()
<< std::endl;
return 0;
}
g++ -std=&#39;c++11&#39; nodes.pb.cc test_proto_json.cpp -lprotobuf -o pj && ./pj
protobuf convert json done!
{
&#34;name&#34;: &#34;111111&#34;,
&#34;addr_info&#34;: {
&#34;bind&#34;: &#34;xxxxxxxxxx&#34;,
&#34;port&#34;: 342,
&#34;gate_bind&#34;: &#34;fsduyruwerw&#34;,
&#34;gate_port&#34;: 4853
},
&#34;node_type&#34;: &#34;34rw343&#34;,
&#34;conf_path&#34;: &#34;reuwyruiwe&#34;,
&#34;work_path&#34;: &#34;ewiruwe&#34;,
&#34;worker_cnt&#34;: 3
}
-----
json to protobuf done!
name: 111111
bind: xxxxxxxxxx3. 参考
- protobuf 官网
- protobuf github
- protobuf json 文档
- c++ - 协议(protocol)buffer3和json
|
|