19 #ifndef GRPC_CORE_LIB_JSON_JSON_UTIL_H
20 #define GRPC_CORE_LIB_JSON_JSON_UTIL_H
24 #include "absl/strings/numbers.h"
25 #include "absl/strings/str_cat.h"
43 template <
typename NumericType,
typename ErrorVectorType>
46 ErrorVectorType* error_list) {
47 static_assert(std::is_integral<NumericType>::value,
"Integral required");
50 absl::StrCat(
"field:", field_name,
" error:type should be NUMBER")
56 absl::StrCat(
"field:", field_name,
" error:failed to parse.").c_str()));
62 template <
typename ErrorVectorType>
64 bool* output, ErrorVectorType* error_list) {
65 switch (json.
type()) {
74 absl::StrCat(
"field:", field_name,
" error:type should be BOOLEAN")
80 template <
typename ErrorVectorType>
83 ErrorVectorType* error_list) {
87 absl::StrCat(
"field:", field_name,
" error:type should be STRING")
95 template <
typename ErrorVectorType>
98 ErrorVectorType* error_list) {
102 absl::StrCat(
"field:", field_name,
" error:type should be ARRAY")
110 template <
typename ErrorVectorType>
113 ErrorVectorType* error_list) {
117 absl::StrCat(
"field:", field_name,
" error:type should be OBJECT")
125 template <
typename NumericType,
typename ErrorVectorType>
127 NumericType* output, ErrorVectorType* error_list) {
131 template <
typename ErrorVectorType>
133 bool* output, ErrorVectorType* error_list) {
137 template <
typename ErrorVectorType>
139 std::string* output, ErrorVectorType* error_list) {
143 template <
typename ErrorVectorType>
146 ErrorVectorType* error_list) {
150 template <
typename ErrorVectorType>
153 ErrorVectorType* error_list) {
157 template <
typename T,
typename ErrorVectorType>
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()) {
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>
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()) {
185 absl::StrCat(
"field:", field_name,
" error:does not exist.")
193 absl::StrCat(
"field:", field_name,
194 " error:type should be STRING of the form given by "
195 "google.proto.Duration.")
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