Debian Codegraph @

src/core/lib/json/json_util.cc

raw
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
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
38 bool ExtractJsonBool(const Json& json, absl::string_view field_name,
39 bool* output, std::vector<grpc_error_handle>* error_list) {
40 switch (json.type()) {
41 case Json::Type::JSON_TRUE:
42 *output = true;
43 return true;
44 case Json::Type::JSON_FALSE:
45 *output = false;
46 return true;
47 default:
48 error_list->push_back(GRPC_ERROR_CREATE(
49 absl::StrCat("field:", field_name, " error:type should be BOOLEAN")));
50 return false;
51 }
52 }
53
54 bool ExtractJsonArray(const Json& json, absl::string_view field_name,
55 const Json::Array** output,
56 std::vector<grpc_error_handle>* error_list) {
57 if (json.type() != Json::Type::ARRAY) {
58 *output = nullptr;
59 error_list->push_back(GRPC_ERROR_CREATE(
60 absl::StrCat("field:", field_name, " error:type should be ARRAY")));
61 return false;
62 }
63 *output = &json.array_value();
64 return true;
65 }
66
67 bool ExtractJsonObject(const Json& json, absl::string_view field_name,
68 const Json::Object** output,
69 std::vector<grpc_error_handle>* error_list) {
70 if (json.type() != Json::Type::OBJECT) {
71 *output = nullptr;
72 error_list->push_back(GRPC_ERROR_CREATE(
73 absl::StrCat("field:", field_name, " error:type should be OBJECT")));
74 return false;
75 }
76 *output = &json.object_value();
77 return true;
78 }
79
80 bool ParseJsonObjectFieldAsDuration(const Json::Object& object,
81 absl::string_view field_name,
82 Duration* output,
83 std::vector<grpc_error_handle>* error_list,
84 bool required) {
85 // TODO(roth): Once we can use C++14 heterogenous lookups, stop
86 // creating a std::string here.
87 auto it = object.find(std::string(field_name));
88 if (it == object.end()) {
89 if (required) {
90 error_list->push_back(GRPC_ERROR_CREATE(
91 absl::StrCat("field:", field_name, " error:does not exist.")));
92 }
93 return false;
94 }
95 if (!ParseDurationFromJson(it->second, output)) {
96 *output = Duration::NegativeInfinity();
97 error_list->push_back(GRPC_ERROR_CREATE(
98 absl::StrCat("field:", field_name,
99 " error:type should be STRING of the form given by "
100 "google.proto.Duration.")));
101 return false;
102 }
103 return true;
104 }
105
106 } // namespace grpc_core