-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathjson.hpp
230 lines (179 loc) · 5.57 KB
/
json.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#pragma once
#include <memory>
#include <string>
#include <stdexcept>
#include <boost/lexical_cast.hpp>
#include <rapidjson/document.h>
#include "blackhole/config/factory.hpp"
#include "blackhole/config/json.hpp"
#include "blackhole/config/node.hpp"
#include "blackhole/config/option.hpp"
#include "factory.hpp"
namespace blackhole {
inline namespace v1 {
namespace config {
using blackhole::config::make_option;
using blackhole::config::node_t;
typedef blackhole::config::option<node_t> option_t;
class type_mismatch : public std::logic_error {
struct {
std::string cursor;
std::string expected;
std::string actual;
} data;
public:
type_mismatch(std::string cursor, std::string expected, std::string actual) :
std::logic_error("type mismatch at \"" + cursor + "\": " +
"expected \"" + expected + "\", actual \"" + actual + "\"")
{
data.cursor = std::move(cursor);
data.expected = std::move(expected);
data.actual = std::move(actual);
}
type_mismatch(const type_mismatch& other) = default;
type_mismatch(type_mismatch&& other) = default;
~type_mismatch() noexcept {}
auto expected() const -> std::string {
return data.expected;
}
auto actual() const -> std::string {
return data.actual;
}
auto cursor() const -> std::string {
return data.cursor;
}
};
class json_t : public node_t {
const rapidjson::Value& value;
std::string cursor;
public:
explicit json_t(const rapidjson::Value& value) :
value(value)
{}
json_t(const rapidjson::Value& value, std::string cursor) :
value(value),
cursor(std::move(cursor))
{}
auto is_bool() const noexcept -> bool {
return value.IsBool();
}
auto is_sint64() const noexcept -> bool {
return value.IsInt64();
}
auto is_uint64() const noexcept -> bool {
return value.IsUint64();
}
auto is_double() const noexcept -> bool {
return value.IsDouble();
}
auto is_string() const noexcept -> bool {
return value.IsString();
}
auto is_vector() const noexcept -> bool {
return value.IsArray();
}
auto is_object() const noexcept -> bool {
return value.IsObject();
}
auto to_bool() const -> bool {
if (is_bool()) {
return value.GetBool();
}
type_mismatch("bool");
}
auto to_sint64() const -> std::int64_t {
if (is_sint64()) {
return value.GetInt64();
}
type_mismatch("int64");
}
auto to_uint64() const -> std::uint64_t {
if (is_uint64()) {
return value.GetUint64();
}
type_mismatch("uint64");
}
auto to_double() const -> double {
if (is_double()) {
return value.GetDouble();
}
type_mismatch("double");
}
auto to_string() const -> std::string {
if (is_string()) {
return value.GetString();
}
type_mismatch("string");
}
auto each(const each_function& fn) const -> void {
if (!value.IsArray()) {
type_mismatch("array");
}
for (auto it = value.Begin(); it != value.End(); ++it) {
fn(json_t(*it, advance(static_cast<std::size_t>(std::distance(value.Begin(), it)))));
}
}
auto each_map(const member_function& fn) const -> void {
if (!value.IsObject()) {
type_mismatch("object");
}
for (auto it = value.MemberBegin(); it != value.MemberEnd(); ++it) {
fn(it->name.GetString(), json_t(it->value, advance(it->name.GetString())));
}
}
auto operator[](const std::size_t& idx) const -> option_t {
if (value.IsArray() && idx < value.Size()) {
return make_option<json_t>(value[static_cast<rapidjson::SizeType>(idx)], advance(idx));
}
return {};
}
auto operator[](const std::string& key) const -> option_t {
if (value.IsObject() && value.HasMember(key.c_str())) {
return make_option<json_t>(value[key.c_str()], advance(key));
}
return {};
}
private:
auto advance(const std::size_t& idx) const -> std::string {
return cursor + "/" + boost::lexical_cast<std::string>(idx);
}
auto advance(const std::string& key) const -> std::string {
return cursor + "/" + key;
}
__attribute((noreturn)) auto type_mismatch(const std::string& expected) const -> void {
throw config::type_mismatch(cursor.empty() ? "/" : cursor, expected, type());
}
auto type() const -> std::string {
switch (value.GetType()) {
case rapidjson::kNullType:
return "null";
case rapidjson::kTrueType:
case rapidjson::kFalseType:
return "bool";
case rapidjson::kNumberType:
return "number";
case rapidjson::kStringType:
return "string";
case rapidjson::kArrayType:
return "array";
case rapidjson::kObjectType:
return "object";
}
}
};
/// Represents a JSON based logger configuration factory.
template<>
class factory<json_t> : public factory_t {
rapidjson::Document doc;
const json_t node;
public:
explicit factory(std::istream& stream);
explicit factory(std::istream&& stream);
/// Returns a const lvalue reference to the root configuration.
auto config() const noexcept -> const node_t&;
private:
auto initialize(std::istream& stream) -> void;
};
} // namespace config
} // namespace v1
} // namespace blackhole