簡體   English   中英

協議buffer3和json

[英]Protocol buffer3 and json

協議緩沖區v3聲稱該庫是json友好的( https://developers.google.com/protocol-buffers/docs/proto3#json ),但是我找不到如何實現該映射的方法。 我應該在協議中添加一些插件或選項,還是調用一些特殊的東西而不是SerializeTo / ParseFrom?

使用該功能的人嗎?

我正在使用Protobuf 3.3.0,它確實具有內置的JSON序列化器和解析器。 您可以使用google/protobuf/util/json_util.h 2個函數MessageToJsonString()JsonStringToMessage()來使C ++生成的Message對象分別進入JSON和從JSON獲得。

這是使用它們的簡單測試: test-protobuf.proto

syntax = "proto3";

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

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;
}

您可以使用以下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}
)

假設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

您應該看到以下輸出:

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

Hello!, 1, 10

Protobuf具有用於C#的json api。 Google protobuf參考資料中有一些C#的json類,您可以在github protobuf存儲庫中找到一些針對Java和c ++的測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM