Imported Upstream version 1.41.0
[platform/upstream/grpc.git] / src / core / lib / json / json_util.h
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
22 #include <grpc/support/port_platform.h>
23
24 #include "absl/strings/numbers.h"
25 #include "absl/strings/str_cat.h"
26
27 #include "src/core/lib/iomgr/exec_ctx.h"
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_CPP_STRING(
50         absl::StrCat("field:", field_name, " error:type should be NUMBER")));
51     return false;
52   }
53   if (!absl::SimpleAtoi(json.string_value(), output)) {
54     error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
55         absl::StrCat("field:", field_name, " error:failed to parse.")));
56     return false;
57   }
58   return true;
59 }
60
61 template <typename ErrorVectorType>
62 inline bool ExtractJsonBool(const Json& json, const std::string& field_name,
63                             bool* output, ErrorVectorType* error_list) {
64   switch (json.type()) {
65     case Json::Type::JSON_TRUE:
66       *output = true;
67       return true;
68     case Json::Type::JSON_FALSE:
69       *output = false;
70       return true;
71     default:
72       error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
73           absl::StrCat("field:", field_name, " error:type should be BOOLEAN")));
74       return false;
75   }
76 }
77
78 template <typename ErrorVectorType>
79 inline bool ExtractJsonString(const Json& json, const std::string& field_name,
80                               std::string* output,
81                               ErrorVectorType* error_list) {
82   if (json.type() != Json::Type::STRING) {
83     *output = "";
84     error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
85         absl::StrCat("field:", field_name, " error:type should be STRING")));
86     return false;
87   }
88   *output = json.string_value();
89   return true;
90 }
91
92 template <typename ErrorVectorType>
93 inline bool ExtractJsonArray(const Json& json, const std::string& field_name,
94                              const Json::Array** output,
95                              ErrorVectorType* error_list) {
96   if (json.type() != Json::Type::ARRAY) {
97     *output = nullptr;
98     error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
99         absl::StrCat("field:", field_name, " error:type should be ARRAY")));
100     return false;
101   }
102   *output = &json.array_value();
103   return true;
104 }
105
106 template <typename ErrorVectorType>
107 inline bool ExtractJsonObject(const Json& json, const std::string& field_name,
108                               const Json::Object** output,
109                               ErrorVectorType* error_list) {
110   if (json.type() != Json::Type::OBJECT) {
111     *output = nullptr;
112     error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
113         absl::StrCat("field:", field_name, " error:type should be OBJECT")));
114     return false;
115   }
116   *output = &json.object_value();
117   return true;
118 }
119
120 template <typename NumericType, typename ErrorVectorType>
121 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
122                             NumericType* output, ErrorVectorType* error_list) {
123   return ExtractJsonNumber(json, field_name, output, error_list);
124 }
125
126 template <typename ErrorVectorType>
127 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
128                             bool* output, ErrorVectorType* error_list) {
129   return ExtractJsonBool(json, field_name, output, error_list);
130 }
131
132 template <typename ErrorVectorType>
133 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
134                             std::string* output, ErrorVectorType* error_list) {
135   return ExtractJsonString(json, field_name, output, error_list);
136 }
137
138 template <typename ErrorVectorType>
139 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
140                             const Json::Array** output,
141                             ErrorVectorType* error_list) {
142   return ExtractJsonArray(json, field_name, output, error_list);
143 }
144
145 template <typename ErrorVectorType>
146 inline bool ExtractJsonType(const Json& json, const std::string& field_name,
147                             const Json::Object** output,
148                             ErrorVectorType* error_list) {
149   return ExtractJsonObject(json, field_name, output, error_list);
150 }
151
152 template <typename T, typename ErrorVectorType>
153 inline bool ParseJsonObjectField(const Json::Object& object,
154                                  const std::string& field_name, T* output,
155                                  ErrorVectorType* error_list,
156                                  bool required = true) {
157   auto it = object.find(field_name);
158   if (it == object.end()) {
159     if (required) {
160       error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
161           absl::StrCat("field:", field_name, " error:does not exist.")));
162     }
163     return false;
164   }
165   auto& child_object_json = it->second;
166   return ExtractJsonType(child_object_json, field_name, output, error_list);
167 }
168
169 template <typename ErrorVectorType>
170 inline bool ParseJsonObjectFieldAsDuration(const Json::Object& object,
171                                            const std::string& field_name,
172                                            grpc_millis* output,
173                                            ErrorVectorType* error_list,
174                                            bool required = true) {
175   auto it = object.find(field_name);
176   if (it == object.end()) {
177     if (required) {
178       error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
179           absl::StrCat("field:", field_name, " error:does not exist.")));
180     }
181     return false;
182   }
183   if (!ParseDurationFromJson(it->second, output)) {
184     *output = GRPC_MILLIS_INF_PAST;
185     error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
186         absl::StrCat("field:", field_name,
187                      " error:type should be STRING of the form given by "
188                      "google.proto.Duration.")));
189     return false;
190   }
191   return true;
192 }
193
194 }  // namespace grpc_core
195
196 #endif  // GRPC_CORE_LIB_JSON_JSON_UTIL_H