Skip to content
Snippets Groups Projects
protobuf.cc 784 B
Newer Older
  • Learn to ignore specific revisions
  • #include <util/protobuf.hh>
    
    #include <google/protobuf/util/json_util.h>
    #include <fmt/core.h>
    
    #include "catch.hpp"
    
    namespace vereign {
    namespace test {
    
    std::string ProtobufToJson(const google::protobuf::Message& msg) {
      google::protobuf::util::JsonPrintOptions options;
      options.preserve_proto_field_names = true;
      options.always_print_primitive_fields = true;
      options.always_print_enums_as_ints = true;
      options.add_whitespace = true;
    
      std::string json;
      auto status = google::protobuf::util::MessageToJsonString(
        msg,
        &json,
        options
      );
    
      if (!status.ok()) {
        FAIL(
          fmt::format(
            "request json encode failed: {}",
            std::string(status.error_message())
          )
        );
      }
    
      return json;
    }
    
    } // namespace vereign
    } // namespace test