Debian Codegraph @

test/cpp/qps/parse_json.cc

raw
1 /*
2 *
3 * Copyright 2016 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include "test/cpp/qps/parse_json.h"
20
21 #include <string>
22
23 #include <grpc/support/log.h>
24
25 namespace grpc {
26 namespace testing {
27
28 void ParseJson(const std::string& json, const std::string& type,
29 GRPC_CUSTOM_MESSAGE* msg) {
30 std::unique_ptr<protobuf::json::TypeResolver> type_resolver(
31 protobuf::json::NewTypeResolverForDescriptorPool(
32 "type.googleapis.com", protobuf::DescriptorPool::generated_pool()));
33 std::string binary;
34 auto status = JsonToBinaryString(
35 type_resolver.get(), "type.googleapis.com/" + type, json, &binary);
36 if (!status.ok()) {
37 std::string errmsg(status.message());
38 gpr_log(GPR_ERROR, "Failed to convert json to binary: errcode=%d msg=%s",
39 status.code(), errmsg.c_str());
40 gpr_log(GPR_ERROR, "JSON: %s", json.c_str());
41 abort();
42 }
43 GPR_ASSERT(msg->ParseFromString(binary));
44 }
45
46 std::string SerializeJson(const GRPC_CUSTOM_MESSAGE& msg,
47 const std::string& type) {
48 std::unique_ptr<protobuf::json::TypeResolver> type_resolver(
49 protobuf::json::NewTypeResolverForDescriptorPool(
50 "type.googleapis.com", protobuf::DescriptorPool::generated_pool()));
51 std::string binary;
52 std::string json_string;
53 msg.SerializeToString(&binary);
54 auto status =
55 BinaryToJsonString(type_resolver.get(), type, binary, &json_string);
56 GPR_ASSERT(status.ok());
57 return json_string;
58 }
59
60 } // namespace testing
61 } // namespace grpc