GRPC C++  1.74.0
json.h
Go to the documentation of this file.
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_SUPPORT_JSON_H
18 #define GRPC_SUPPORT_JSON_H
19 
21 #include <stdint.h>
22 
23 #include <map>
24 #include <string>
25 #include <type_traits>
26 #include <utility>
27 #include <variant>
28 #include <vector>
29 
30 #include "absl/strings/str_cat.h"
31 
32 namespace grpc_core {
33 namespace experimental {
34 
35 // A JSON value, which can be any one of null, boolean, number, string,
36 // object, or array.
37 class Json {
38  public:
39  // The JSON type.
40  enum class Type {
41  kNull, // No payload. Default type when using the zero-arg ctor.
42  kBoolean, // Use boolean() for payload.
43  kNumber, // Numbers are stored in string form to avoid precision
44  // and integer capacity issues. Use string() for payload.
45  kString, // Use string() for payload.
46  kObject, // Use object() for payload.
47  kArray, // Use array() for payload.
48  };
49 
50  template <typename Sink>
51  friend void AbslStringify(Sink& sink, Type type) {
52  switch (type) {
53  case Type::kNull:
54  sink.Append("null");
55  break;
56  case Type::kBoolean:
57  sink.Append("boolean");
58  break;
59  case Type::kNumber:
60  sink.Append("number");
61  break;
62  case Type::kString:
63  sink.Append("string");
64  break;
65  case Type::kObject:
66  sink.Append("object");
67  break;
68  case Type::kArray:
69  sink.Append("array");
70  break;
71  }
72  }
73 
74  using Object = std::map<std::string, Json>;
75  using Array = std::vector<Json>;
76 
77  // Factory method for kBoolean.
78  static Json FromBool(bool b) {
79  Json json;
80  json.value_ = b;
81  return json;
82  }
83 
84  // Factory methods for kNumber.
85  static Json FromNumber(const std::string& str) {
86  Json json;
87  json.value_ = NumberValue{str};
88  return json;
89  }
90  static Json FromNumber(const char* str) {
91  Json json;
92  json.value_ = NumberValue{std::string(str)};
93  return json;
94  }
95  static Json FromNumber(std::string&& str) {
96  Json json;
97  json.value_ = NumberValue{std::move(str)};
98  return json;
99  }
100  template <typename T>
101  static std::enable_if_t<std::is_arithmetic_v<T>, Json> FromNumber(T value) {
102  Json json;
103  json.value_ = NumberValue{absl::StrCat(value)};
104  return json;
105  }
106 
107  // Factory methods for kString.
108  static Json FromString(const std::string& str) {
109  Json json;
110  json.value_ = str;
111  return json;
112  }
113  static Json FromString(const char* str) {
114  Json json;
115  json.value_ = std::string(str);
116  return json;
117  }
118  static Json FromString(std::string&& str) {
119  Json json;
120  json.value_ = std::move(str);
121  return json;
122  }
123 
124  // Factory methods for kObject.
125  static Json FromObject(const Object& object) {
126  Json json;
127  json.value_ = object;
128  return json;
129  }
130  static Json FromObject(Object&& object) {
131  Json json;
132  json.value_ = std::move(object);
133  return json;
134  }
135 
136  // Factory methods for kArray.
137  static Json FromArray(const Array& array) {
138  Json json;
139  json.value_ = array;
140  return json;
141  }
142  static Json FromArray(Array&& array) {
143  Json json;
144  json.value_ = std::move(array);
145  return json;
146  }
147 
148  Json() = default;
149 
150  // Copyable.
151  Json(const Json& other) = default;
152  Json& operator=(const Json& other) = default;
153 
154  // Moveable.
155  Json(Json&& other) noexcept : value_(std::move(other.value_)) {
156  other.value_ = std::monostate();
157  }
158  Json& operator=(Json&& other) noexcept {
159  value_ = std::move(other.value_);
160  other.value_ = std::monostate();
161  return *this;
162  }
163 
164  // Returns the JSON type.
165  Type type() const {
166  struct ValueFunctor {
167  Json::Type operator()(const std::monostate&) { return Type::kNull; }
168  Json::Type operator()(bool) { return Type::kBoolean; }
169  Json::Type operator()(const NumberValue&) { return Type::kNumber; }
170  Json::Type operator()(const std::string&) { return Type::kString; }
171  Json::Type operator()(const Object&) { return Type::kObject; }
172  Json::Type operator()(const Array&) { return Type::kArray; }
173  };
174  return std::visit(ValueFunctor(), value_);
175  }
176 
177  // Payload accessor for kBoolean.
178  // Must not be called for other types.
179  bool boolean() const { return std::get<bool>(value_); }
180 
181  // Payload accessor for kNumber or kString.
182  // Must not be called for other types.
183  const std::string& string() const {
184  const NumberValue* num = std::get_if<NumberValue>(&value_);
185  if (num != nullptr) return num->value;
186  return std::get<std::string>(value_);
187  }
188 
189  // Payload accessor for kObject.
190  // Must not be called for other types.
191  const Object& object() const { return std::get<Object>(value_); }
192 
193  // Payload accessor for kArray.
194  // Must not be called for other types.
195  const Array& array() const { return std::get<Array>(value_); }
196 
197  bool operator==(const Json& other) const { return value_ == other.value_; }
198  bool operator!=(const Json& other) const { return !(*this == other); }
199 
200  private:
201  struct NumberValue {
202  std::string value;
203 
204  bool operator==(const NumberValue& other) const {
205  return value == other.value;
206  }
207  };
208  using Value = std::variant<std::monostate, // kNull
209  bool, // kBoolean
210  NumberValue, // kNumber
211  std::string, // kString
212  Object, // kObject
213  Array>; // kArray
214 
215  explicit Json(Value value) : value_(std::move(value)) {}
216 
217  Value value_;
218 };
219 
220 } // namespace experimental
221 } // namespace grpc_core
222 
223 #endif // GRPC_SUPPORT_JSON_H
grpc_core::experimental::Json::FromArray
static Json FromArray(Array &&array)
Definition: json.h:142
grpc_core
Definition: grpc_audit_logging.h:31
grpc_core::experimental::Json::FromString
static Json FromString(std::string &&str)
Definition: json.h:118
grpc_core::experimental::Json::Array
std::vector< Json > Array
Definition: json.h:75
grpc_core::experimental::Json::boolean
bool boolean() const
Definition: json.h:179
grpc_core::experimental::Json::FromString
static Json FromString(const std::string &str)
Definition: json.h:108
grpc_core::experimental::Json::Type::kNull
@ kNull
grpc_core::experimental::Json::AbslStringify
friend void AbslStringify(Sink &sink, Type type)
Definition: json.h:51
grpc_core::experimental::Json::FromNumber
static Json FromNumber(std::string &&str)
Definition: json.h:95
grpc_core::experimental::Json::object
const Object & object() const
Definition: json.h:191
grpc_core::experimental::Json::operator=
Json & operator=(const Json &other)=default
grpc_core::experimental::Json::Object
std::map< std::string, Json > Object
Definition: json.h:74
grpc_core::experimental::Json::FromNumber
static Json FromNumber(const char *str)
Definition: json.h:90
grpc_core::experimental::Json::Json
Json()=default
grpc_core::experimental::Json::array
const Array & array() const
Definition: json.h:195
grpc_core::experimental::Json
Definition: json.h:37
grpc_core::experimental::Json::Type::kString
@ kString
grpc_core::experimental::Json::FromNumber
static std::enable_if_t< std::is_arithmetic_v< T >, Json > FromNumber(T value)
Definition: json.h:101
grpc_core::experimental::Json::Type
Type
Definition: json.h:40
grpc_core::experimental::Json::operator==
bool operator==(const Json &other) const
Definition: json.h:197
grpc_core::experimental::Json::Type::kNumber
@ kNumber
grpc_core::experimental::Json::FromBool
static Json FromBool(bool b)
Definition: json.h:78
grpc_core::experimental::Json::FromNumber
static Json FromNumber(const std::string &str)
Definition: json.h:85
grpc_core::experimental::Json::operator!=
bool operator!=(const Json &other) const
Definition: json.h:198
grpc_core::experimental::Json::Type::kArray
@ kArray
grpc_core::experimental::Json::FromObject
static Json FromObject(const Object &object)
Definition: json.h:125
std
Definition: async_unary_call.h:406
grpc_core::experimental::Json::FromArray
static Json FromArray(const Array &array)
Definition: json.h:137
grpc_core::experimental::Json::FromString
static Json FromString(const char *str)
Definition: json.h:113
grpc_core::experimental::Json::Json
Json(Json &&other) noexcept
Definition: json.h:155
grpc_core::experimental::Json::Type::kBoolean
@ kBoolean
grpc_core::experimental::Json::type
Type type() const
Definition: json.h:165
grpc_core::experimental::Json::operator=
Json & operator=(Json &&other) noexcept
Definition: json.h:158
grpc_core::experimental::Json::string
const std::string & string() const
Definition: json.h:183
grpc_core::experimental::Json::FromObject
static Json FromObject(Object &&object)
Definition: json.h:130
grpc_core::experimental::Json::Type::kObject
@ kObject
port_platform.h