14 using value_type = Json::Value;
15 using string_type = std::string;
18 using value_type = Json::Value;
22 explicit array_type(
const Json::Value& o) : Json::Value(o) {}
24 explicit array_type(Json::Value&& o) : Json::Value(o) {}
25 template<
typename Iterator>
27 for (Iterator it = begin; it != end; ++it) {
37 using number_type = double;
38 using integer_type = Json::Value::Int;
39 using boolean_type = bool;
42 using key_type = std::string;
43 using mapped_type = Json::Value;
44 using size_type = size_t;
48 explicit object_type(
const Json::Value& o) : Json::Value(o) {}
50 explicit object_type(Json::Value&& o) : Json::Value(o) {}
56 const mapped_type& at(
const key_type& key)
const {
57 Json::Value
const* found = find(key.data(), key.data() + key.length());
58 if (!found)
throw std::out_of_range(
"invalid key");
62 size_type count(
const key_type& key)
const {
return this->isMember(key) ? 1 : 0; }
71 else if (val.isString())
76 else if (val.isNumeric())
78 else if (val.isBool())
80 else if (val.isObject())
83 throw std::logic_error(
"invalid type");
86 static integer_type as_integer(
const value_type& val) {
88 case Json::intValue:
return val.asInt64();
89 case Json::uintValue:
return static_cast<integer_type
>(val.asUInt64());
90 default:
throw std::bad_cast();
94 static boolean_type as_boolean(
const value_type& val) {
95 if (!val.isBool())
throw std::bad_cast();
99 static number_type as_number(
const value_type& val) {
100 if (!val.isNumeric())
throw std::bad_cast();
101 return val.asDouble();
104 static string_type as_string(
const value_type& val) {
105 if (!val.isString())
throw std::bad_cast();
106 return val.asString();
109 static object_type as_object(
const value_type& val) {
110 if (!val.isObject())
throw std::bad_cast();
111 return object_type(val);
114 static array_type as_array(
const value_type& val) {
115 if (!val.isArray())
throw std::bad_cast();
116 return array_type(val);
119 static bool parse(value_type& val, string_type str) {
120 Json::CharReaderBuilder builder;
121 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
123 return reader->parse(
reinterpret_cast<const char*
>(str.c_str()),
124 reinterpret_cast<const char*
>(str.c_str() + str.size()), &val,
nullptr);
127 static string_type serialize(
const value_type& val) {
128 Json::StreamWriterBuilder builder;
129 builder[
"commentStyle"] =
"None";
130 builder[
"indentation"] =
"";
131 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
132 return Json::writeString(builder, val);