-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectBuilder.cpp
79 lines (49 loc) · 1.56 KB
/
ObjectBuilder.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
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
#include "ObjectBuilder.hpp"
#include <boost/json.hpp>
#include <boost/system/detail/errc.hpp>
#include "boost/property_tree/ptree.hpp"
#include "boost/property_tree/json_parser.hpp"
#include <fstream>
#include <iostream>
#include <iterator>
#include <streambuf>
#include <string>
#include <cerrno>
namespace KDeTLearning
{
namespace errc = boost::system::errc;
std::string getFileContents(const std::string &fileName)
{
std::ifstream theFile(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
theFile.exceptions(theFile.badbit);
std::ifstream::pos_type fileSize = theFile.tellg();
theFile.seekg(0, std::ios::beg);
std::vector<char> bytes(fileSize);
theFile.read(bytes.data(), fileSize);
return std::string(bytes.data(), fileSize);
}
void parse(const std::string &fileName)
{
boost::json::error_code error;
boost::json::stream_parser theParser;
std::string fileContents = getFileContents(fileName);
theParser.write(fileContents, error);
if(errc::success == error) {
theParser.finish(error);
auto theThing = theParser.release();
auto theString = boost::json::serialize(theThing);
std::cout << theString << std::endl;
} else {
throw(std::domain_error("Parse error"));
}
}
void tryParseTree(const std::string &fileName)
{
boost::property_tree::ptree pt;
boost::property_tree::read_json(fileName, pt);
for(auto &v : pt.get_child("particles")) {
std::cout << v.first << " : " << v.second.data() << std::endl;
}
return;
}
}