GRPC Core  48.0.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  using Object = std::map<std::string, Json>;
51  using Array = std::vector<Json>;
52 
53  // Factory method for kBoolean.
54  static Json FromBool(bool b) {
55  Json json;
56  json.value_ = b;
57  return json;
58  }
59 
60  // Factory methods for kNumber.
61  static Json FromNumber(const std::string& str) {
62  Json json;
63  json.value_ = NumberValue{str};
64  return json;
65  }
66  static Json FromNumber(const char* str) {
67  Json json;
68  json.value_ = NumberValue{std::string(str)};
69  return json;
70  }
71  static Json FromNumber(std::string&& str) {
72  Json json;
73  json.value_ = NumberValue{std::move(str)};
74  return json;
75  }
76  template <typename T>
77  static std::enable_if_t<std::is_arithmetic_v<T>, Json> FromNumber(T value) {
78  Json json;
79  json.value_ = NumberValue{absl::StrCat(value)};
80  return json;
81  }
82 
83  // Factory methods for kString.
84  static Json FromString(const std::string& str) {
85  Json json;
86  json.value_ = str;
87  return json;
88  }
89  static Json FromString(const char* str) {
90  Json json;
91  json.value_ = std::string(str);
92  return json;
93  }
94  static Json FromString(std::string&& str) {
95  Json json;
96  json.value_ = std::move(str);
97  return json;
98  }
99 
100  // Factory methods for kObject.
101  static Json FromObject(const Object& object) {
102  Json json;
103  json.value_ = object;
104  return json;
105  }
106  static Json FromObject(Object&& object) {
107  Json json;
108  json.value_ = std::move(object);
109  return json;
110  }
111 
112  // Factory methods for kArray.
113  static Json FromArray(const Array& array) {
114  Json json;
115  json.value_ = array;
116  return json;
117  }
118  static Json FromArray(Array&& array) {
119  Json json;
120  json.value_ = std::move(array);
121  return json;
122  }
123 
124  Json() = default;
125 
126  // Copyable.
127  Json(const Json& other) = default;
128  Json& operator=(const Json& other) = default;
129 
130  // Moveable.
131  Json(Json&& other) noexcept : value_(std::move(other.value_)) {
132  other.value_ = std::monostate();
133  }
134  Json& operator=(Json&& other) noexcept {
135  value_ = std::move(other.value_);
136  other.value_ = std::monostate();
137  return *this;
138  }
139 
140  // Returns the JSON type.
141  Type type() const {
142  struct ValueFunctor {
143  Json::Type operator()(const std::monostate&) { return Type::kNull; }
144  Json::Type operator()(bool) { return Type::kBoolean; }
145  Json::Type operator()(const NumberValue&) { return Type::kNumber; }
146  Json::Type operator()(const std::string&) { return Type::kString; }
147  Json::Type operator()(const Object&) { return Type::kObject; }
148  Json::Type operator()(const Array&) { return Type::kArray; }
149  };
150  return std::visit(ValueFunctor(), value_);
151  }
152 
153  // Payload accessor for kBoolean.
154  // Must not be called for other types.
155  bool boolean() const { return std::get<bool>(value_); }
156 
157  // Payload accessor for kNumber or kString.
158  // Must not be called for other types.
159  const std::string& string() const {
160  const NumberValue* num = std::get_if<NumberValue>(&value_);
161  if (num != nullptr) return num->value;
162  return std::get<std::string>(value_);
163  }
164 
165  // Payload accessor for kObject.
166  // Must not be called for other types.
167  const Object& object() const { return std::get<Object>(value_); }
168 
169  // Payload accessor for kArray.
170  // Must not be called for other types.
171  const Array& array() const { return std::get<Array>(value_); }
172 
173  bool operator==(const Json& other) const { return value_ == other.value_; }
174  bool operator!=(const Json& other) const { return !(*this == other); }
175 
176  private:
177  struct NumberValue {
178  std::string value;
179 
180  bool operator==(const NumberValue& other) const {
181  return value == other.value;
182  }
183  };
184  using Value = std::variant<std::monostate, // kNull
185  bool, // kBoolean
186  NumberValue, // kNumber
187  std::string, // kString
188  Object, // kObject
189  Array>; // kArray
190 
191  explicit Json(Value value) : value_(std::move(value)) {}
192 
193  Value value_;
194 };
195 
196 } // namespace experimental
197 } // namespace grpc_core
198 
199 #endif // GRPC_SUPPORT_JSON_H
grpc_core::experimental::Json::FromArray
static Json FromArray(Array &&array)
Definition: json.h:118
grpc_core
Definition: grpc_audit_logging.h:31
grpc_core::experimental::Json::FromString
static Json FromString(std::string &&str)
Definition: json.h:94
grpc_core::experimental::Json::Array
std::vector< Json > Array
Definition: json.h:51
grpc_core::experimental::Json::boolean
bool boolean() const
Definition: json.h:155
grpc_core::experimental::Json::FromString
static Json FromString(const std::string &str)
Definition: json.h:84
grpc_core::experimental::Json::Type::kNull
@ kNull
grpc_core::experimental::Json::FromNumber
static Json FromNumber(std::string &&str)
Definition: json.h:71
grpc_core::experimental::Json::object
const Object & object() const
Definition: json.h:167
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:50
grpc_core::experimental::Json::FromNumber
static Json FromNumber(const char *str)
Definition: json.h:66
grpc_core::experimental::Json::Json
Json()=default
grpc_core::experimental::Json::array
const Array & array() const
Definition: json.h:171
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:77
grpc_core::experimental::Json::Type
Type
Definition: json.h:40
grpc_core::experimental::Json::operator==
bool operator==(const Json &other) const
Definition: json.h:173
grpc_core::experimental::Json::Type::kNumber
@ kNumber
grpc_core::experimental::Json::FromBool
static Json FromBool(bool b)
Definition: json.h:54
grpc_core::experimental::Json::FromNumber
static Json FromNumber(const std::string &str)
Definition: json.h:61
grpc_core::experimental::Json::operator!=
bool operator!=(const Json &other) const
Definition: json.h:174
grpc_core::experimental::Json::Type::kArray
@ kArray
grpc_core::experimental::Json::FromObject
static Json FromObject(const Object &object)
Definition: json.h:101
grpc_core::experimental::Json::FromArray
static Json FromArray(const Array &array)
Definition: json.h:113
grpc_core::experimental::Json::FromString
static Json FromString(const char *str)
Definition: json.h:89
grpc_core::experimental::Json::Json
Json(Json &&other) noexcept
Definition: json.h:131
grpc_core::experimental::Json::Type::kBoolean
@ kBoolean
grpc_core::experimental::Json::type
Type type() const
Definition: json.h:141
grpc_core::experimental::Json::operator=
Json & operator=(Json &&other) noexcept
Definition: json.h:134
grpc_core::experimental::Json::string
const std::string & string() const
Definition: json.h:159
grpc_core::experimental::Json::FromObject
static Json FromObject(Object &&object)
Definition: json.h:106
grpc_core::experimental::Json::Type::kObject
@ kObject
port_platform.h