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_CPP_STRING(
50 absl::StrCat("field:", field_name, " error:type should be NUMBER")));
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.")));
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:
68 case Json::Type::JSON_FALSE:
72 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
73 absl::StrCat("field:", field_name, " error:type should be BOOLEAN")));
78 template <typename ErrorVectorType>
79 inline bool ExtractJsonString(const Json& json, const std::string& field_name,
81 ErrorVectorType* error_list) {
82 if (json.type() != Json::Type::STRING) {
84 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
85 absl::StrCat("field:", field_name, " error:type should be STRING")));
88 *output = json.string_value();
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) {
98 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
99 absl::StrCat("field:", field_name, " error:type should be ARRAY")));
102 *output = &json.array_value();
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) {
112 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
113 absl::StrCat("field:", field_name, " error:type should be OBJECT")));
116 *output = &json.object_value();
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);
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);
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);
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);
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);
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()) {
160 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
161 absl::StrCat("field:", field_name, " error:does not exist.")));
165 auto& child_object_json = it->second;
166 return ExtractJsonType(child_object_json, field_name, output, error_list);
169 template <typename ErrorVectorType>
170 inline bool ParseJsonObjectFieldAsDuration(const Json::Object& object,
171 const std::string& field_name,
173 ErrorVectorType* error_list,
174 bool required = true) {
175 auto it = object.find(field_name);
176 if (it == object.end()) {
178 error_list->push_back(GRPC_ERROR_CREATE_FROM_CPP_STRING(
179 absl::StrCat("field:", field_name, " error:does not exist.")));
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.")));
194 } // namespace grpc_core
196 #endif // GRPC_CORE_LIB_JSON_JSON_UTIL_H