简体   繁体   English

协议buffer3和json

[英]Protocol buffer3 and json

Protocol buffer v3 claims, that library is json friendly ( https://developers.google.com/protocol-buffers/docs/proto3#json ), but I cannot find how to achieve get that mapping. 协议缓冲区v3声称该库是json友好的( https://developers.google.com/protocol-buffers/docs/proto3#json ),但是我找不到如何实现该映射的方法。 Should I add some plugin, or some option into protoc, or call something special instead SerializeTo/ParseFrom? 我应该在协议中添加一些插件或选项,还是调用一些特殊的东西而不是SerializeTo / ParseFrom?

Is it someone who use that feature? 使用该功能的人吗?

I'm using Protobuf 3.3.0, which does have a built-in JSON serializer and parser. 我正在使用Protobuf 3.3.0,它确实具有内置的JSON序列化器和解析器。 You can use 2 functions from google/protobuf/util/json_util.h called MessageToJsonString() and JsonStringToMessage() to make your C++ generated Message objects go to and from JSON respectively. 您可以使用google/protobuf/util/json_util.h 2个函数MessageToJsonString()JsonStringToMessage()来使C ++生成的Message对象分别进入JSON和从JSON获得。

Here's a simple test that uses them: test-protobuf.proto : 这是使用它们的简单测试: test-protobuf.proto

syntax = "proto3";

message SearchRequest {
  string query = 1;
  int32 page_number = 2;
  int32 result_per_page = 3;
}

test-protobuf.cpp : test-protobuf.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;
}

You can compile these files (assuming that you have protobuf, a compiler, and CMake installed) by using the following CMakeLists.txt file (tested on Windows). 您可以使用以下CMakeLists.txt文件(在Windows上经过测试)来编译这些文件(假设您已安装protobuf,编译器和CMake)。

cmake_minimum_required(VERSION 3.8)

project(test-protobuf)

find_package(Protobuf REQUIRED)

# Use static runtime for MSVC
if(MSVC)
  foreach(flag_var
      CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
      CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
    if(${flag_var} MATCHES "/MD")
      string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
    endif(${flag_var} MATCHES "/MD")
  endforeach(flag_var)
endif(MSVC)

protobuf_generate_cpp(test-protobuf-sources test-protobuf-headers
  "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.proto"
)

list(APPEND test-protobuf-sources
  "${CMAKE_CURRENT_LIST_DIR}/test-protobuf.cpp"
)

add_executable(test-protobuf ${test-protobuf-sources} ${test-protobuf-headers})
target_include_directories(test-protobuf
  PUBLIC
    ${PROTOBUF_INCLUDE_DIRS}
    ${CMAKE_CURRENT_BINARY_DIR}
)
target_link_libraries(test-protobuf
  ${PROTOBUF_LIBRARIES}
)

Assuming that CMakeLists.txt , test-protobuf.proto , and test-protobuf.cpp are in the same directory, here are the commands to compile and run them on Windows with Visual Studio 15 2017 and 64-bit protobuf libraries. 假设CMakeLists.txttest-protobuf.prototest-protobuf.cpp位于同一目录中,以下是在带有Visual Studio 15 2017和64位protobuf库的Windows上编译和运行它们的命令。

mkdir build
cd build
cmake -G "Visual Studio 15 2017 Win64" ..
cmake --build . --config Release
Release/test-protobuf

You should see the following output: 您应该看到以下输出:

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

Hello!, 1, 10

Protobuf has json api for C#. Protobuf具有用于C#的json api。 There are some json class for C# in google protobuf reference and You can find some tests in github protobuf repository for java and c++. Google protobuf参考资料中有一些C#的json类,您可以在github protobuf存储库中找到一些针对Java和c ++的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM