-
Notifications
You must be signed in to change notification settings - Fork 57
/
json.cpp
52 lines (40 loc) · 1.32 KB
/
json.cpp
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
#include "json.hpp"
#include <fstream>
#include <rapidjson/document.h>
#include <rapidjson/error/en.h>
#include <rapidjson/istreamwrapper.h>
#include "../memory.hpp"
namespace blackhole {
inline namespace v1 {
namespace config {
auto factory_traits<json_t>::construct(std::istream& stream) -> std::unique_ptr<factory_t> {
return blackhole::make_unique<factory<json_t>>(stream);
}
auto factory_traits<json_t>::construct(std::istream&& stream) -> std::unique_ptr<factory_t> {
return blackhole::make_unique<factory<json_t>>(std::move(stream));
}
factory<json_t>::factory(std::istream& stream) :
node(doc)
{
initialize(stream);
}
factory<json_t>::factory(std::istream&& stream) :
node(doc)
{
initialize(stream);
}
auto factory<json_t>::config() const noexcept -> const node_t& {
return node;
}
auto factory<json_t>::initialize(std::istream& stream) -> void {
rapidjson::IStreamWrapper wrapper(stream);
doc.ParseStream<rapidjson::kParseCommentsFlag | rapidjson::kParseTrailingCommasFlag>(wrapper);
if (doc.HasParseError()) {
throw std::invalid_argument("parse error at offset " +
boost::lexical_cast<std::string>(doc.GetErrorOffset()) +
": " + rapidjson::GetParseError_En(doc.GetParseError()));
}
}
} // namespace config
} // namespace v1
} // namespace blackhole