-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson_utils.cpp
171 lines (115 loc) · 3.62 KB
/
json_utils.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
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
// FIXME exception text could be improved
#include <cstring>
#include <sstream>
#include <fstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include "rf_pipelines_internals.hpp"
using namespace std;
namespace rf_pipelines {
#if 0
}; // pacify emacs c-mode!
#endif
string json_stringify(const Json::Value &j)
{
Json::StyledWriter w;
return w.write(j);
}
Json::Value json_read(const std::string &filename, bool noisy)
{
ifstream f(filename);
if (f.fail())
throw runtime_error(filename + ": couldn't open file");
Json::Value j;
try {
f >> j;
} catch (...) {
throw runtime_error(filename + ": couldn't parse json file");
}
if (noisy)
cout << "read " << filename << endl;
return j;
}
void json_write(const string &filename, const Json::Value &j, bool noisy)
{
ofstream f(filename);
if (f.fail())
throw runtime_error(filename + ": couldn't open file for writing");
f << json_stringify(j);
if (noisy)
cout << "wrote " << filename << endl;
}
static const Json::Value &get_member(const Json::Value &j, const string &k)
{
if (!j.isObject())
throw runtime_error("rf_pipelines: json value was not an Object as expected");
if (!j.isMember(k))
throw runtime_error("rf_pipelines: json field '" + k + "' was expected but not found");
return j[k];
}
string string_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isString())
throw runtime_error("rf_pipelines: json field '" + k + "' was not a string as expected");
return v.asString();
}
int int_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isIntegral())
throw runtime_error("rf_pipelines: json field '" + k + "' was not an integer as expected");
return v.asInt();
}
bool bool_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isBool())
throw runtime_error("rf_pipelines: json field '" + k + "' was not boolean as expected");
return v.asBool();
}
ssize_t ssize_t_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isIntegral())
throw runtime_error("rf_pipelines: json field '" + k + "' was not an integer as expected");
return v.asInt64();
}
uint64_t uint64_t_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isIntegral())
throw runtime_error("rf_pipelines: json field '" + k + "' was not an integer as expected");
return v.asUInt64();
}
double double_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isDouble())
throw runtime_error("rf_pipelines: json field '" + k + "' was not a floating-point number as expected");
return v.asDouble();
}
rf_kernels::axis_type axis_type_from_json(const Json::Value &j, const string &k)
{
string s = string_from_json(j, k);
return rf_kernels::axis_type_from_string(s, "json field");
}
Json::Value array_from_json(const Json::Value &j, const string &k)
{
const Json::Value &v = get_member(j, k);
if (!v.isArray())
throw runtime_error("rf_pipelines: json field '" + k + "' was not an array as expected");
return v;
}
void add_json_object(Json::Value &dst, const Json::Value &src)
{
if (src.isNull())
return;
if (!src.isObject())
throw runtime_error("rf_pipelines internal error: 'src' argument to add_json_object() was not an Object as expected");
for (const auto &key: src.getMemberNames())
dst[key] = src[key];
}
} // namespace rf_pipelines