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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/json/json_util.h"
22
23 #include "src/core/lib/gprpp/no_destruct.h"
24 #include "src/core/lib/gprpp/validation_errors.h"
25 #include "src/core/lib/json/json_args.h"
26 #include "src/core/lib/json/json_object_loader.h"
27
28 namespace grpc_core {
29
ParseDurationFromJson(const Json & field,Duration * duration)30 bool ParseDurationFromJson(const Json& field, Duration* duration) {
31 ValidationErrors errors;
32 static_cast<json_detail::LoaderInterface*>(
33 NoDestructSingleton<json_detail::AutoLoader<Duration>>::Get())
34 ->LoadInto(field, JsonArgs(), duration, &errors);
35 return errors.ok();
36 }
37
ExtractJsonBool(const Json & json,absl::string_view field_name,bool * output,std::vector<grpc_error_handle> * error_list)38 bool ExtractJsonBool(const Json& json, absl::string_view field_name,
39 bool* output, std::vector<grpc_error_handle>* error_list) {
40 if (json.type() != Json::Type::kBoolean) {
41 error_list->push_back(GRPC_ERROR_CREATE(
42 absl::StrCat("field:", field_name, " error:type should be BOOLEAN")));
43 return false;
44 }
45 *output = json.boolean();
46 return true;
47 }
48
ExtractJsonArray(const Json & json,absl::string_view field_name,const Json::Array ** output,std::vector<grpc_error_handle> * error_list)49 bool ExtractJsonArray(const Json& json, absl::string_view field_name,
50 const Json::Array** output,
51 std::vector<grpc_error_handle>* error_list) {
52 if (json.type() != Json::Type::kArray) {
53 *output = nullptr;
54 error_list->push_back(GRPC_ERROR_CREATE(
55 absl::StrCat("field:", field_name, " error:type should be ARRAY")));
56 return false;
57 }
58 *output = &json.array();
59 return true;
60 }
61
ExtractJsonObject(const Json & json,absl::string_view field_name,const Json::Object ** output,std::vector<grpc_error_handle> * error_list)62 bool ExtractJsonObject(const Json& json, absl::string_view field_name,
63 const Json::Object** output,
64 std::vector<grpc_error_handle>* error_list) {
65 if (json.type() != Json::Type::kObject) {
66 *output = nullptr;
67 error_list->push_back(GRPC_ERROR_CREATE(
68 absl::StrCat("field:", field_name, " error:type should be OBJECT")));
69 return false;
70 }
71 *output = &json.object();
72 return true;
73 }
74
ParseJsonObjectFieldAsDuration(const Json::Object & object,absl::string_view field_name,Duration * output,std::vector<grpc_error_handle> * error_list,bool required)75 bool ParseJsonObjectFieldAsDuration(const Json::Object& object,
76 absl::string_view field_name,
77 Duration* output,
78 std::vector<grpc_error_handle>* error_list,
79 bool required) {
80 // TODO(roth): Once we can use C++14 heterogenous lookups, stop
81 // creating a std::string here.
82 auto it = object.find(std::string(field_name));
83 if (it == object.end()) {
84 if (required) {
85 error_list->push_back(GRPC_ERROR_CREATE(
86 absl::StrCat("field:", field_name, " error:does not exist.")));
87 }
88 return false;
89 }
90 if (!ParseDurationFromJson(it->second, output)) {
91 *output = Duration::NegativeInfinity();
92 error_list->push_back(GRPC_ERROR_CREATE(
93 absl::StrCat("field:", field_name,
94 " error:type should be STRING of the form given by "
95 "google.proto.Duration.")));
96 return false;
97 }
98 return true;
99 }
100
101 } // namespace grpc_core
102