17 #ifndef GRPC_SUPPORT_JSON_H
18 #define GRPC_SUPPORT_JSON_H
25 #include <type_traits>
30 #include "absl/strings/str_cat.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)};
79 json.value_ = NumberValue{absl::StrCat(value)};
91 json.value_ = std::string(str);
96 json.value_ = std::move(str);
108 json.value_ = std::move(
object);
120 json.value_ = std::move(
array);
131 Json(
Json&& other) noexcept : value_(std::move(other.value_)) {
132 other.value_ = std::monostate();
135 value_ = std::move(other.value_);
136 other.value_ = std::monostate();
142 struct ValueFunctor {
150 return std::visit(ValueFunctor(), value_);
155 bool boolean()
const {
return std::get<bool>(value_); }
160 const NumberValue* num = std::get_if<NumberValue>(&value_);
161 if (num !=
nullptr)
return num->value;
162 return std::get<std::string>(value_);
171 const Array&
array()
const {
return std::get<Array>(value_); }
180 bool operator==(
const NumberValue& other)
const {
181 return value == other.value;
184 using Value = std::variant<std::monostate,
191 explicit Json(Value value) : value_(std::move(value)) {}
199 #endif // GRPC_SUPPORT_JSON_H