15 using value_type = Json::Value;
16 using string_type = std::string;
17 class array_type :
public Json::Value {
19 using value_type = Json::Value;
21 array_type() =
default;
22 array_type(
const array_type&) =
default;
23 explicit array_type(
const Json::Value& o) : Json::Value(o) {}
24 array_type(array_type&&) =
default;
25 explicit array_type(Json::Value&& o) : Json::Value(o) {}
26 array_type(std::initializer_list<value_type> init) {
27 for (
auto const& v : init) {
33 template<
typename Iterator>
34 array_type(Iterator begin, Iterator end) {
35 for (Iterator it = begin; it != end; ++it) {
41 ~array_type() =
default;
42 array_type& operator=(
const array_type& o) =
default;
43 array_type& operator=(array_type&& o)
noexcept =
default;
45 value_type
const& front()
const {
return this->operator[](0U); }
47 using number_type = double;
48 using integer_type = Json::Value::Int;
49 using boolean_type = bool;
50 class object_type :
public Json::Value {
52 using key_type = std::string;
53 using mapped_type = Json::Value;
54 using size_type = size_t;
56 object_type() : Json::Value(Json::objectValue) {}
57 object_type(
const object_type&) =
default;
58 explicit object_type(
const Json::Value& o) : Json::Value(o) {}
59 object_type(object_type&&) =
default;
60 explicit object_type(Json::Value&& o) : Json::Value(o) {}
61 ~object_type() =
default;
62 object_type& operator=(
const object_type& o) =
default;
63 object_type& operator=(object_type&& o)
noexcept =
default;
66 const mapped_type& at(
const key_type& key)
const {
67 Json::Value
const* found = find(key.data(), key.data() + key.length());
68 if (!found)
throw std::out_of_range(
"invalid key");
72 size_type count(
const key_type& key)
const {
return this->isMember(key) ? 1 : 0; }
81 else if (val.isString())
86 else if (val.isNumeric())
88 else if (val.isBool())
90 else if (val.isObject())
93 throw std::logic_error(
"invalid type");
96 static integer_type as_integer(
const value_type& val) {
98 case Json::intValue:
return val.asInt64();
99 case Json::uintValue:
return static_cast<integer_type
>(val.asUInt64());
100 default:
throw std::bad_cast();
104 static boolean_type as_boolean(
const value_type& val) {
105 if (!val.isBool())
throw std::bad_cast();
109 static number_type as_number(
const value_type& val) {
110 if (!val.isNumeric())
throw std::bad_cast();
111 return val.asDouble();
114 static string_type as_string(
const value_type& val) {
115 if (!val.isString())
throw std::bad_cast();
116 return val.asString();
119 static object_type as_object(
const value_type& val) {
120 if (!val.isObject())
throw std::bad_cast();
124 static array_type as_array(
const value_type& val) {
125 if (!val.isArray())
throw std::bad_cast();
129 static bool parse(value_type& val, string_type str) {
130 Json::CharReaderBuilder builder;
131 const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
133 return reader->parse(
reinterpret_cast<const char*
>(str.c_str()),
134 reinterpret_cast<const char*
>(str.c_str() + str.size()), &val,
nullptr);
137 static string_type serialize(
const value_type& val) {
138 Json::StreamWriterBuilder builder;
139 builder[
"commentStyle"] =
"None";
140 builder[
"indentation"] =
"";
141 std::unique_ptr<Json::StreamWriter> writer(builder.newStreamWriter());
142 return Json::writeString(builder, val);