Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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
C++ 协议缓冲区3和json_C++_Json_Protocol Buffers_Proto3 - Fatal编程技术网

C++ 协议缓冲区3和json

C++ 协议缓冲区3和json,c++,json,protocol-buffers,proto3,C++,Json,Protocol Buffers,Proto3,ProtocolBufferV3声称,该库是json友好的(),但我找不到如何实现该映射。我应该在protoc中添加一些插件或选项,还是调用一些特殊的东西来代替SerializeTo/ParseFrom 是不是有人使用了这个功能?Protobuf有C#的json api。有一些JSON类用于C++,可以在java和C++中找到一些测试。p> 我正在使用Protobuf3.3.0,它有一个内置的JSON序列化程序和解析器。您可以使用代码2 >谷歌/OptubF/UTIL/JSONUTIL.H./调

ProtocolBufferV3声称,该库是json友好的(),但我找不到如何实现该映射。我应该在protoc中添加一些插件或选项,还是调用一些特殊的东西来代替SerializeTo/ParseFrom


是不是有人使用了这个功能?

Protobuf有C#的json api。有一些JSON类用于C++,可以在java和C++中找到一些测试。p> 我正在使用Protobuf3.3.0,它有一个内置的JSON序列化程序和解析器。您可以使用代码2 >谷歌/OptubF/UTIL/JSONUTIL.H./<代码>调用<代码> MasaGeTJS音讯()/<代码>和代码> JSON StReTraceMeX()/Cuff> >使您的C++生成的<>代码> />代码>对象分别从JSON和.P> 下面是一个使用它们的简单测试:
testprotobuf.proto

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}
测试协议cpp

#include <iostream>
#include <google/protobuf/util/json_util.h>

#include "test-protobuf.pb.h"

int main()
{
  std::string json_string;
  SearchRequest sr, sr2;

  // Populate sr.
  sr.set_query(std::string("Hello!"));
  sr.set_page_number(1);
  sr.set_result_per_page(10);

  // Create a json_string from sr.
  google::protobuf::util::JsonPrintOptions options;
  options.add_whitespace = true;
  options.always_print_primitive_fields = true;
  options.preserve_proto_field_names = true;
  MessageToJsonString(sr, &json_string, options);

  // Print json_string.
  std::cout << json_string << std::endl;


  // Parse the json_string into sr2.
  google::protobuf::util::JsonParseOptions options2;
  JsonStringToMessage(json_string, &sr2, options2);

  // Print the values of sr2.
  std::cout
    << sr2.query() << ", "
    << sr2.page_number() << ", "
    << sr2.result_per_page() << std::endl
  ;

  return 0;
}
假设
CMakeLists.txt
test protobuf.proto
test protobuf.cpp
位于同一目录中,下面是在Windows上使用Visual Studio 15 2017和64位protobuf库编译和运行它们的命令

mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
cmake --build . --config Release
Release/test-protobuf
您应该看到以下输出:

{
 "query": "Hello!",
 "page_number": 1,
 "result_per_page": 10
}

Hello!, 1, 10

protobuf-C中有没有可能实现JSON转换支持?真的很好,有没有办法用十六进制表示字节值而不是base64?我正在积累protobuf和C的经验。JsonFormatter只接受IMessage作为输入。假设您只有Protobuf消息的某个实例……如何将其转换为IMessage以便将其序列化为Json?下面是Protobuf->Json:string jsonMessage=Google.Protobuf.JsonFormatter.Default.Format((IMessage)person)。。。。。。。。。。。。。来自JSON->Protobuf:IMessage message=(IMessage)Activator.CreateInstance(typeof(Person));Person personFromJSON=(Person)Google.Protobuf.JsonParser.Default.Parse(jsonMessage,message.Descriptor);
{
 "query": "Hello!",
 "page_number": 1,
 "result_per_page": 10
}

Hello!, 1, 10