GRPC Core  18.0.0
json_util.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2020 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 #ifndef GRPC_CORE_LIB_JSON_JSON_UTIL_H
20 #define GRPC_CORE_LIB_JSON_JSON_UTIL_H
21 
23 
24 #include "absl/strings/numbers.h"
25 #include "absl/strings/str_cat.h"
26 
28 #include "src/core/lib/json/json.h"
29 
30 namespace grpc_core {
31 
32 // Parses a JSON field of the form generated for a google.proto.Duration
33 // proto message, as per:
34 // https://developers.google.com/protocol-buffers/docs/proto3#json
35 // Returns true on success, false otherwise.
36 bool ParseDurationFromJson(const Json& field, grpc_millis* duration);
37 
38 //
39 // Helper functions for extracting types from JSON.
40 // Return true on success, false otherwise. If an error is encountered during
41 // parsing, a descriptive error is appended to \a error_list.
42 //
43 template <typename NumericType, typename ErrorVectorType>
44 inline bool ExtractJsonNumber(const Json& json, const std::string& field_name,
45  NumericType* output,
46  ErrorVectorType* error_list) {
47  static_assert(std::is_integral<NumericType>::value, "Integral required");
48  if (json.type() != Json::Type::NUMBER) {
49  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
50  absl::StrCat("field:", field_name, " error:type should be NUMBER")
51  .c_str()));
52  return false;
53  }
54  if (!absl::SimpleAtoi(json.string_value(), output)) {
55  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
56  absl::StrCat("field:", field_name, " error:failed to parse.").c_str()));
57  return false;
58  }
59  return true;
60 }
61 
62 template <typename ErrorVectorType>
63 inline bool ExtractJsonBool(const Json& json, const std::string& field_name,
64  bool* output, ErrorVectorType* error_list) {
65  switch (json.type()) {
67  *output = true;
68  return true;
70  *output = false;
71  return true;
72  default:
73  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
74  absl::StrCat("field:", field_name, " error:type should be BOOLEAN")
75  .c_str()));
76  return false;
77  }
78 }
79 
80 template <typename ErrorVectorType>
81 inline bool ExtractJsonString(const Json& json, const std::string& field_name,
82  std::string* output,
83  ErrorVectorType* error_list) {
84  if (json.type() != Json::Type::STRING) {
85  *output = "";
86  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
87  absl::StrCat("field:", field_name, " error:type should be STRING")
88  .c_str()));
89  return false;
90  }
91  *output = json.string_value();
92  return true;
93 }
94 
95 template <typename ErrorVectorType>
96 inline bool ExtractJsonArray(const Json& json, const std::string& field_name,
97  const Json::Array** output,
98  ErrorVectorType* error_list) {
99  if (json.type() != Json::Type::ARRAY) {
100  *output = nullptr;
101  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
102  absl::StrCat("field:", field_name, " error:type should be ARRAY")
103  .c_str()));
104  return false;
105  }
106  *output = &json.array_value();
107  return true;
108 }
109 
110 template <typename ErrorVectorType>
111 inline bool ExtractJsonObject(const Json& json, const std::string& field_name,
112  const Json::Object** output,
113  ErrorVectorType* error_list) {
114  if (json.type() != Json::Type::OBJECT) {
115  *output = nullptr;
116  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
117  absl::StrCat("field:", field_name, " error:type should be OBJECT")
118  .c_str()));
119  return false;
120  }
121  *output = &json.object_value();
122  return true;
123 }
124 
125 template <typename NumericType, typename ErrorVectorType>
126 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
127  NumericType* output, ErrorVectorType* error_list) {
128  return ExtractJsonNumber(json, field_name, output, error_list);
129 }
130 
131 template <typename ErrorVectorType>
132 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
133  bool* output, ErrorVectorType* error_list) {
134  return ExtractJsonBool(json, field_name, output, error_list);
135 }
136 
137 template <typename ErrorVectorType>
138 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
139  std::string* output, ErrorVectorType* error_list) {
140  return ExtractJsonString(json, field_name, output, error_list);
141 }
142 
143 template <typename ErrorVectorType>
144 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
145  const Json::Array** output,
146  ErrorVectorType* error_list) {
147  return ExtractJsonArray(json, field_name, output, error_list);
148 }
149 
150 template <typename ErrorVectorType>
151 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
152  const Json::Object** output,
153  ErrorVectorType* error_list) {
154  return ExtractJsonObject(json, field_name, output, error_list);
155 }
156 
157 template <typename T, typename ErrorVectorType>
158 inline bool ParseJsonObjectField(const Json::Object& object,
159  const std::string& field_name, T* output,
160  ErrorVectorType* error_list,
161  bool required = true) {
162  auto it = object.find(field_name);
163  if (it == object.end()) {
164  if (required) {
165  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
166  absl::StrCat("field:", field_name, " error:does not exist.")
167  .c_str()));
168  }
169  return false;
170  }
171  auto& child_object_json = it->second;
172  return ExtractJsonType(child_object_json, field_name, output, error_list);
173 }
174 
175 template <typename ErrorVectorType>
177  const std::string& field_name,
178  grpc_millis* output,
179  ErrorVectorType* error_list,
180  bool required = true) {
181  auto it = object.find(field_name);
182  if (it == object.end()) {
183  if (required) {
184  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
185  absl::StrCat("field:", field_name, " error:does not exist.")
186  .c_str()));
187  }
188  return false;
189  }
190  if (!ParseDurationFromJson(it->second, output)) {
191  *output = GRPC_MILLIS_INF_PAST;
192  error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
193  absl::StrCat("field:", field_name,
194  " error:type should be STRING of the form given by "
195  "google.proto.Duration.")
196  .c_str()));
197  return false;
198  }
199  return true;
200 }
201 
202 } // namespace grpc_core
203 
204 #endif // GRPC_CORE_LIB_JSON_JSON_UTIL_H
Definition: json.h:38
Type type() const
Definition: json.h:175
const std::string & string_value() const
Definition: json.h:176
const Array & array_value() const
Definition: json.h:180
std::map< std::string, Json > Object
Definition: json.h:55
const Object & object_value() const
Definition: json.h:178
std::vector< Json > Array
Definition: json.h:56
#define GRPC_ERROR_CREATE_FROM_COPIED_STRING(desc)
Definition: error.h:286
int64_t grpc_millis
Definition: exec_ctx.h:37
#define GRPC_MILLIS_INF_PAST
Definition: exec_ctx.h:40
Round Robin Policy.
Definition: backend_metric.cc:26
bool ExtractJsonArray(const Json &json, const std::string &field_name, const Json::Array **output, ErrorVectorType *error_list)
Definition: json_util.h:96
bool ParseDurationFromJson(const Json &field, grpc_millis *duration)
Definition: json_util.cc:29
bool ParseJsonObjectFieldAsDuration(const Json::Object &object, const std::string &field_name, grpc_millis *output, ErrorVectorType *error_list, bool required=true)
Definition: json_util.h:176
bool ExtractJsonObject(const Json &json, const std::string &field_name, const Json::Object **output, ErrorVectorType *error_list)
Definition: json_util.h:111
bool ParseJsonObjectField(const Json::Object &object, const std::string &field_name, T *output, ErrorVectorType *error_list, bool required=true)
Definition: json_util.h:158
bool ExtractJsonString(const Json &json, const std::string &field_name, std::string *output, ErrorVectorType *error_list)
Definition: json_util.h:81
bool ExtractJsonNumber(const Json &json, const std::string &field_name, NumericType *output, ErrorVectorType *error_list)
Definition: json_util.h:44
bool ExtractJsonType(const Json &json, const std::string &field_name, NumericType *output, ErrorVectorType *error_list)
Definition: json_util.h:126
bool ExtractJsonBool(const Json &json, const std::string &field_name, bool *output, ErrorVectorType *error_list)
Definition: json_util.h:63