-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjson.h
353 lines (340 loc) · 16.4 KB
/
json.h
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
Pheniqs : PHilology ENcoder wIth Quality Statistics
Copyright (C) 2018 Lior Galanti
NYU Center for Genetics and System Biology
Author: Lior Galanti <lior.galanti@nyu.edu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PHENIQS_JSON_H
#define PHENIQS_JSON_H
#include "include.h"
#include "error.h"
#include "kstring.h"
class ValidationError : public Error {
public:
Document ontology;
ValidationError(Document& ontology) :
Error("JSON directive validation error", ErrorCode::JSON_VALIDATION_ERROR),
ontology(std::move(ontology)) {
compile();
};
ValidationError(const ValidationError& other) :
Error("JSON directive validation error", ErrorCode::JSON_VALIDATION_ERROR),
ontology(kObjectType) {
ontology.CopyFrom(other.ontology, ontology.GetAllocator());
compile();
};
ostream& describe(ostream& o) const override;
private:
void compile();
};
Document encode_validation_error(const SchemaValidator& validator, const Value& schema, const Value& container);
void print_json(const Value& node, const char* path, const int32_t& precision=PHENIQS_FLOAT_PRECISION);
void print_json(const Value& node, ostream& o=cout, const int32_t& precision=PHENIQS_FLOAT_PRECISION);
// Document* load_json(const string& path);
/* Recursively merge two JSON documents.
Overlay < base > on < ontology >, using < document > for memory allocation.
ontology is updated in the process.
*/
void merge_json_value(const Value& base, Value& ontology, Document& document);
void project_json_value(const Value& base, const Value& ontology, Value& container, Document& document);
void clean_json_value(Value& ontology, Document& document);
void sort_json_value(Value& ontology, Document& document);
void clean_json_object(Value& ontology, Document& document);
void overlay_json_object(Document& ontology, const Value& overlay);
bool remove_disabled_from_json_value(Value& ontology, Document& document);
/* decoding JSON container into an object */
template < typename T > bool decode_value(T& value, const Value& container);
template < typename T > bool decode_value_by_key(const Value::Ch* key, T& value, const Value& container);
template < typename T > T decode_value(const Value& container);
template < typename T > T decode_value_by_key(const Value::Ch* key, const Value& container);
// template < typename T > T decode_value(const Value& container) {
// T value;
// if(!decode_value(value, container)) {
// throw ConfigurationError("value decoding failed");
// }
// return value;
// };
// template < typename T > T decode_value_by_key(const Value::Ch* key, const Value& container) {
// T value;
// if(container.IsObject()) {
// if(!decode_value_by_key(key, value, container)) {
// throw ConfigurationError("unknown key " + string(key));
// }
// } else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
// return value;
// };
/* encoding object to JSON container */
inline bool encode_key_value(const string& key, const bool& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(value).Move(), document.GetAllocator());
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return true;
};
inline bool encode_key_value(const string& key, const uint8_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value < numeric_limits< uint8_t >::max()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(static_cast< unsigned>(value)).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< uint8_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(static_cast< unsigned >(v)).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const list< uint8_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(static_cast< unsigned>(v)).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const int32_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value < numeric_limits< int32_t >::max()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(static_cast< int >(value)).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< int32_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const uint32_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value < numeric_limits< uint32_t >::max()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(static_cast< unsigned>(value)).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< uint32_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(static_cast< unsigned>(v)).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const int64_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value < numeric_limits< int64_t >::max()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(value).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< int64_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const list< int64_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const uint64_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value < numeric_limits< uint64_t >::max()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(value).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< uint64_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const list< uint64_t >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const double& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(value != numeric_limits< double >::infinity()) {
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), Value(value).Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< double >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const list< double >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const string& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
container.AddMember (
Value(key.c_str(), key.size(), document.GetAllocator()).Move(),
Value(value.c_str(),value.length(), document.GetAllocator()).Move(),
document.GetAllocator()
);
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const vector< string >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v.c_str(), v.length(), document.GetAllocator()).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const list< string >& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(!value.empty()) {
Value array(kArrayType);
for(auto& v : value) {
array.PushBack(Value(v.c_str(), v.length(), document.GetAllocator()).Move(), document.GetAllocator());
}
container.AddMember(Value(key.c_str(), key.size(), document.GetAllocator()).Move(), array.Move(), document.GetAllocator());
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
inline bool encode_key_value(const string& key, const kstring_t& value, Value& container, Document& document) {
if(container.IsObject()) {
container.RemoveMember(key.c_str());
if(ks_not_empty(value)) {
container.AddMember (
Value(key.c_str(), key.size(), document.GetAllocator()).Move(),
Value(value.s, value.l, document.GetAllocator()).Move(),
document.GetAllocator()
);
return true;
}
} else { throw ConfigurationError(string(key) + " container is not a dictionary"); }
return false;
};
#endif /* PHENIQS_JSON_H */