Debian Codegraph @

src/core/lib/json/json.h

raw
1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #ifndef GRPC_CORE_LIB_JSON_JSON_H
18 #define GRPC_CORE_LIB_JSON_JSON_H
19
20 #include <grpc/support/port_platform.h>
21
22 #include <map>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 #include "absl/status/statusor.h"
28 #include "absl/strings/string_view.h"
29
30 namespace grpc_core {
31
32 // A JSON value, which can be any one of object, array, string,
33 // number, true, false, or null.
34 class Json {
35 public:
36 // TODO(roth): Currently, numbers are stored internally as strings,
37 // which makes the API a bit cumbersome to use. When we have time,
38 // consider whether there's a better alternative (e.g., maybe storing
39 // each numeric type as the native C++ type and automatically converting
40 // to string as needed).
41 enum class Type {
42 JSON_NULL,
43 JSON_TRUE,
44 JSON_FALSE,
45 NUMBER,
46 STRING,
47 OBJECT,
48 ARRAY
49 };
50
51 using Object = std::map<std::string, Json>;
52 using Array = std::vector<Json>;
53
54 // Parses JSON string from json_str.
55 static absl::StatusOr<Json> Parse(absl::string_view json_str);
56
57 Json() = default;
58
59 // Copyable.
60 Json(const Json& other) { CopyFrom(other); }
61 Json& operator=(const Json& other) {
62 CopyFrom(other);
63 return *this;
64 }
65
66 // Moveable.
67 Json(Json&& other) noexcept { MoveFrom(std::move(other)); }
68 Json& operator=(Json&& other) noexcept {
69 MoveFrom(std::move(other));
70 return *this;
71 }
72
73 // Construct from copying a string.
74 // If is_number is true, the type will be NUMBER instead of STRING.
75 // NOLINTNEXTLINE(google-explicit-constructor)
76 Json(const std::string& string, bool is_number = false)
77 : type_(is_number ? Type::NUMBER : Type::STRING), string_value_(string) {}
78 Json& operator=(const std::string& string) {
79 type_ = Type::STRING;
80 string_value_ = string;
81 return *this;
82 }
83
84 // Same thing for C-style strings, both const and mutable.
85 // NOLINTNEXTLINE(google-explicit-constructor)
86 Json(const char* string, bool is_number = false)
87 : Json(std::string(string), is_number) {}
88 Json& operator=(const char* string) {
89 *this = std::string(string);
90 return *this;
91 }
92 // NOLINTNEXTLINE(google-explicit-constructor)
93 Json(char* string, bool is_number = false)
94 : Json(std::string(string), is_number) {}
95 Json& operator=(char* string) {
96 *this = std::string(string);
97 return *this;
98 }
99
100 // Construct by moving a string.
101 // NOLINTNEXTLINE(google-explicit-constructor)
102 Json(std::string&& string)
103 : type_(Type::STRING), string_value_(std::move(string)) {}
104 Json& operator=(std::string&& string) {
105 type_ = Type::STRING;
106 string_value_ = std::move(string);
107 return *this;
108 }
109
110 // Construct from bool.
111 // NOLINTNEXTLINE(google-explicit-constructor)
112 Json(bool b) : type_(b ? Type::JSON_TRUE : Type::JSON_FALSE) {}
113 Json& operator=(bool b) {
114 type_ = b ? Type::JSON_TRUE : Type::JSON_FALSE;
115 return *this;
116 }
117
118 // Construct from any numeric type.
119 template <typename NumericType>
120 // NOLINTNEXTLINE(google-explicit-constructor)
121 Json(NumericType number)
122 : type_(Type::NUMBER), string_value_(std::to_string(number)) {}
123 template <typename NumericType>
124 Json& operator=(NumericType number) {
125 type_ = Type::NUMBER;
126 string_value_ = std::to_string(number);
127 return *this;
128 }
129
130 // Construct by copying object.
131 // NOLINTNEXTLINE(google-explicit-constructor)
132 Json(const Object& object) : type_(Type::OBJECT), object_value_(object) {}
133 Json& operator=(const Object& object) {
134 type_ = Type::OBJECT;
135 object_value_ = object;
136 return *this;
137 }
138
139 // Construct by moving object.
140 // NOLINTNEXTLINE(google-explicit-constructor)
141 Json(Object&& object)
142 : type_(Type::OBJECT), object_value_(std::move(object)) {}
143 Json& operator=(Object&& object) {
144 type_ = Type::OBJECT;
145 object_value_ = std::move(object);
146 return *this;
147 }
148
149 // Construct by copying array.
150 // NOLINTNEXTLINE(google-explicit-constructor)
151 Json(const Array& array) : type_(Type::ARRAY), array_value_(array) {}
152 Json& operator=(const Array& array) {
153 type_ = Type::ARRAY;
154 array_value_ = array;
155 return *this;
156 }
157
158 // Construct by moving array.
159 // NOLINTNEXTLINE(google-explicit-constructor)
160 Json(Array&& array) : type_(Type::ARRAY), array_value_(std::move(array)) {}
161 Json& operator=(Array&& array) {
162 type_ = Type::ARRAY;
163 array_value_ = std::move(array);
164 return *this;
165 }
166
167 // Dumps JSON from value to string form.
168 std::string Dump(int indent = 0) const;
169
170 // Accessor methods.
171 Type type() const { return type_; }
172 const std::string& string_value() const { return string_value_; }
173 std::string* mutable_string_value() { return &string_value_; }
174 const Object& object_value() const { return object_value_; }
175 Object* mutable_object() { return &object_value_; }
176 const Array& array_value() const { return array_value_; }
177 Array* mutable_array() { return &array_value_; }
178
179 bool operator==(const Json& other) const {
180 if (type_ != other.type_) return false;
181 switch (type_) {
182 case Type::NUMBER:
183 case Type::STRING:
184 if (string_value_ != other.string_value_) return false;
185 break;
186 case Type::OBJECT:
187 if (object_value_ != other.object_value_) return false;
188 break;
189 case Type::ARRAY:
190 if (array_value_ != other.array_value_) return false;
191 break;
192 default:
193 break;
194 }
195 return true;
196 }
197
198 bool operator!=(const Json& other) const { return !(*this == other); }
199
200 private:
201 void CopyFrom(const Json& other) {
202 type_ = other.type_;
203 switch (type_) {
204 case Type::NUMBER:
205 case Type::STRING:
206 string_value_ = other.string_value_;
207 break;
208 case Type::OBJECT:
209 object_value_ = other.object_value_;
210 break;
211 case Type::ARRAY:
212 array_value_ = other.array_value_;
213 break;
214 default:
215 break;
216 }
217 }
218
219 void MoveFrom(Json&& other) {
220 type_ = other.type_;
221 other.type_ = Type::JSON_NULL;
222 switch (type_) {
223 case Type::NUMBER:
224 case Type::STRING:
225 string_value_ = std::move(other.string_value_);
226 break;
227 case Type::OBJECT:
228 object_value_ = std::move(other.object_value_);
229 break;
230 case Type::ARRAY:
231 array_value_ = std::move(other.array_value_);
232 break;
233 default:
234 break;
235 }
236 }
237
238 Type type_ = Type::JSON_NULL;
239 std::string string_value_;
240 Object object_value_;
241 Array array_value_;
242 };
243
244 } // namespace grpc_core
245
246 #endif // GRPC_CORE_LIB_JSON_JSON_H