17 #ifndef GRPC_SUPPORT_JSON_H
18 #define GRPC_SUPPORT_JSON_H
27 #include "absl/strings/str_cat.h"
28 #include "absl/types/variant.h"
33 namespace experimental {
50 using Object = std::map<std::string, Json>;
51 using Array = std::vector<Json>;
63 json.value_ = NumberValue{str};
68 json.value_ = NumberValue{std::string(str)};
73 json.value_ = NumberValue{std::move(str)};
78 json.value_ = NumberValue{absl::StrCat(value)};
83 json.value_ = NumberValue{absl::StrCat(value)};
88 json.value_ = NumberValue{absl::StrCat(value)};
93 json.value_ = NumberValue{absl::StrCat(value)};
98 json.value_ = NumberValue{absl::StrCat(value)};
110 json.value_ = std::string(str);
115 json.value_ = std::move(str);
127 json.value_ = std::move(
object);
139 json.value_ = std::move(
array);
150 Json(
Json&& other) noexcept : value_(std::move(other.value_)) {
151 other.value_ = absl::monostate();
154 value_ = std::move(other.value_);
155 other.value_ = absl::monostate();
161 struct ValueFunctor {
169 return absl::visit(ValueFunctor(), value_);
174 bool boolean()
const {
return absl::get<bool>(value_); }
179 const NumberValue* num = absl::get_if<NumberValue>(&value_);
180 if (num !=
nullptr)
return num->value;
181 return absl::get<std::string>(value_);
190 const Array&
array()
const {
return absl::get<Array>(value_); }
199 bool operator==(
const NumberValue& other)
const {
200 return value == other.value;
203 using Value = absl::variant<absl::monostate,
210 explicit Json(Value value) : value_(
std::move(value)) {}
218 #endif // GRPC_SUPPORT_JSON_H