3 // Copyright 2020 gRPC authors.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPC_CORE_LIB_JSON_JSON_UTIL_H
20 #define GRPC_CORE_LIB_JSON_JSON_UTIL_H
22 #include <grpc/support/port_platform.h>
24 #include "absl/strings/numbers.h"
25 #include "absl/strings/str_cat.h"
27 #include "src/core/lib/iomgr/exec_ctx.h"
28 #include "src/core/lib/json/json.h"
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);
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.
43 template <typename NumericType, typename ErrorVectorType>
44 inline bool ExtractJsonNumber(const Json& json, const std::string& field_name,
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")
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()));
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()) {
66 case Json::Type::JSON_TRUE:
69 case Json::Type::JSON_FALSE:
73 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
74 absl::StrCat("field:", field_name, " error:type should be BOOLEAN")
80 template <typename ErrorVectorType>
81 inline bool ExtractJsonString(const Json& json, const std::string& field_name,
83 ErrorVectorType* error_list) {
84 if (json.type() != Json::Type::STRING) {
86 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
87 absl::StrCat("field:", field_name, " error:type should be STRING")
91 *output = json.string_value();
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) {
101 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
102 absl::StrCat("field:", field_name, " error:type should be ARRAY")
106 *output = &json.array_value();
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) {
116 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
117 absl::StrCat("field:", field_name, " error:type should be OBJECT")
121 *output = &json.object_value();
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);
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);
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);
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);
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);
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()) {
165 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
166 absl::StrCat("field:", field_name, " error:does not exist.")
171 auto& child_object_json = it->second;
172 return ExtractJsonType(child_object_json, field_name, output, error_list);
175 template <typename ErrorVectorType>
176 inline bool ParseJsonObjectFieldAsDuration(const Json::Object& object,
177 const std::string& field_name,
179 ErrorVectorType* error_list,
180 bool required = true) {
181 auto it = object.find(field_name);
182 if (it == object.end()) {
184 error_list->push_back(GRPC_ERROR_CREATE_FROM_COPIED_STRING(
185 absl::StrCat("field:", field_name, " error:does not exist.")
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.")
202 } // namespace grpc_core
204 #endif // GRPC_CORE_LIB_JSON_JSON_UTIL_H