-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathconfiguration_JSON.cpp
496 lines (402 loc) · 16.6 KB
/
configuration_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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*---------------------------------------------------------------------------*\
*
* bitpit
*
* Copyright (C) 2015-2021 OPTIMAD engineering Srl
*
* -------------------------------------------------------------------------
* License
* This file is part of bitpit.
*
* bitpit is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License v3 (LGPL)
* as published by the Free Software Foundation.
*
* bitpit 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
*
\*---------------------------------------------------------------------------*/
#include "configuration_JSON.hpp"
#if HAS_RAPIDJSON_LIB
#include "rapidjson/encodedstream.h"
#include <rapidjson/error/en.h>
#include "rapidjson/filereadstream.h"
#include <rapidjson/filewritestream.h>
#include <rapidjson/prettywriter.h>
#include <string>
#include <cstdio>
#include <cassert>
#include <set>
#include <iomanip>
namespace bitpit {
namespace config {
namespace JSON {
/*!
Reading a json dictionary from file and fill a bitpit::Config tree
accordingly.
Inside, the method creates a DOM structure from json file and scan it
using readNode method.
JSON root document object does not have a key name and a version unlike
XML.
Encoding is always of UTF type (8,16,32). Here it is adopted the standard
UTF-8 encoding. JSON dictionary can be written in other UTF format, the
conversion is automatically handled by rapidjson parsing.
\param[in] filename is the path of the JSON file
\param[in,out] rootConfig is a pointer to Config tree that will be used
to store the data read from the document
*/
void readConfiguration(const std::string &filename, Config *rootConfig)
{
if (!rootConfig) {
throw std::runtime_error("JSON::readDoc Null Config tree structure passed");
}
// Open the file
//
// To be compatible with Windows platforms, the file is open as a binary
// file (this works fine also on Posix platforms).
std::FILE *fp = std::fopen(filename.c_str(), "rb");
if (!fp) {
std::string message = "JSON:readDoc impossible to write on file : ";
message += filename.c_str();
throw std::runtime_error(message.c_str());
}
// Create a buffer for reading stream purposes.
std::vector<char> readBuffer(65536);
rapidjson::FileReadStream bis(fp, readBuffer.data(), readBuffer.size());
// Wrap the readstream into an auto encoding handle. This will let
// absorb any UTF encoded file into a regular UTF-8 document.
rapidjson::AutoUTFInputStream<unsigned, rapidjson::FileReadStream> eis(bis);
// Parse UTF file into UTF-8 in memory
rapidjson::Document jsonRoot;
jsonRoot.ParseStream<0, rapidjson::AutoUTF<unsigned> >(eis);
// Close the file
std::fclose(fp);
// Handle parse errors
if (jsonRoot.HasParseError()) {
std::string message = "JSON:readDoc error of type : ";
message += std::string(rapidjson::GetParseError_En(jsonRoot.GetParseError()));
throw std::runtime_error(message.c_str());
}
// Root should be an object, bitpit doens't support root arrays.
assert(jsonRoot.IsObject() && "JSON:readDoc parsed document root is not an object");
// Fill the configuration
readNode("", jsonRoot, rootConfig);
}
/*!
Reading a json dictionary from plain c++ string and fill a bitpit::Config tree
accordingly.
String Encoding is always considered of UTF-8 type .
\param[in] source string with JSON contents
\param[in,out] rootConfig is a pointer to Config tree that will be used
to store the data read from the string
*/
void readBufferConfiguration(const std::string &source, Config *rootConfig)
{
if (!rootConfig) {
throw std::runtime_error("JSON::readDoc Null Config tree structure passed");
}
// Open a UTF-8 compliant rapidjson::StringStream
rapidjson::StringStream str(source.c_str());
// Parse stream directly
rapidjson::Document jsonRoot;
jsonRoot.ParseStream(str);
// Handle parse errors
if (jsonRoot.HasParseError()) {
std::string message = "JSON:readBufferConfiguration error of type : ";
message += std::string(rapidjson::GetParseError_En(jsonRoot.GetParseError()));
throw std::runtime_error(message.c_str());
}
// Root should be an object, bitpit doens't support root arrays.
assert(jsonRoot.IsObject() && "JSON:readBufferConfiguration parsed document root is not an object");
// Fill the configuration
readNode("", jsonRoot, rootConfig);
}
/*!
Read recursively a json object content and fill accordingly the Config tree
branch.
The current method visit internal members of JSON object and parse tehm in
Config::Option or if subnested JSON objects or arrays are present in other
Config::Section through a recursive call to itself. If arrays are present,
MultiSection feature of the bitpit::Config tree must be enabled
The method can be used directly on the root content of the JSON document,
specifying as key an empty string.
BEWARE: JSON array support is limited to arrays of JSON objects. Each object
is converted into a Config::Section and stored with the same key name of the
array (using MultiSection property of Config). Pod JSON arrays (bools, numbers,
strings) are not supported since Config tree does not support MultiOptions.
An attempt to read an array of pod elements will throw an error.
\param[in] key key name associated of the JSON data.
\param[in] value JSON data, can be a pod type (bool, number, null,
str ing) or another JSON object/JSON array of contents. In that case
automatic recursion will occur.
\param[in] config pointer to configuration tree to be filled
*/
void readNode(const std::string &key, const rapidjson::Value &value, Config *config)
{
if (value.IsObject()) {
// Get the section
Config::Section *section;
if (key.empty()) {
// If the key is empty we are reading the root.
section = config;
} else if (!config->isMultiSectionsEnabled() &&config->hasSection(key)) {
// Get the requestd section
section = &(config->getSection(key));
} else {
// Create a new section.
//
// If the name already exists, it will be handled as a multi-section
// in the tree map.
section = &(config->addSection(key));
}
// Read the contents of the section
for (auto itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr) {
readNode(std::string(itr->name.GetString()), itr->value, section);
}
} else if (value.IsArray()) {
// Check if multi-section configurations are supported
bool multiSectionsEnabled = config->isMultiSectionsEnabled();
if (!multiSectionsEnabled) {
throw std::runtime_error("JSON:readNode reading array but Config MultiSection disabled");
}
// Read the array contents
for (auto itr = value.Begin(); itr != value.End(); ++itr) {
if (itr->IsObject()) {
readNode(key, *itr, config);
} else {
throw std::runtime_error("JSON:readNode only arrays of JSON Objects are supported!");
}
}
} else {
// Read a single JSON POD type (bool, numbers, strings null).
config->set(key, decodeValue(value));
}
}
/*!
Write a bitpit::Config tree contents to json formatted dictionary.
NOTE: JSON root document object does not have a key name and a version
unlike XML.
Inside the method creates an empty rapidjson Document, set the root object
and fill it recursively from the Config tree using the writeNode method.
Then, it writes the Document on file with standard encoding of UTF-8 type.
Option prettify controls the json dictionary formatting. If false write it
in a unique compact string, otherwise write it down in a more readable tree
structure.
\param[in] filename is the path to a valid json file.
\param[in] rootConfig pointer to the Config tree to be written on file
\param[in] prettify if set to true, a prettyfied human-readable JSON will
be written, otherwise a compatc JSON will be written
*/
void writeConfiguration(const std::string &filename, const Config *rootConfig, bool prettify)
{
if (!rootConfig) {
throw std::runtime_error("JSON::writeConfiguration Null Config tree structure passed");
}
// DOM Document is GenericDocument<UTF8<>>
rapidjson::Document jsonRoot;
// Get the allocator
rapidjson::Document::AllocatorType &allocator = jsonRoot.GetAllocator();
// Create a root node and recursively fill it
jsonRoot.SetObject();
writeNode(rootConfig, jsonRoot, allocator);
// Write on file
//
// To be compatible with Windows platforms, the file is open as a binary
// file (this works fine also on Posix platforms).
std::FILE *fp = std::fopen(filename.c_str(), "wb");
if (!fp) {
std::string message = "JSON:writeConfiguration impossible to write on file : ";
message += filename.c_str();
throw std::runtime_error(message.c_str());
}
// Create a buffer for writing purposes.
std::vector<char> writeBuffer(65536);
rapidjson::FileWriteStream bos(fp, writeBuffer.data(), writeBuffer.size());
// Write the file
if (prettify) {
// Use prettyfied UTF-8 JSON format with default indentantion
rapidjson::PrettyWriter<rapidjson::FileWriteStream> writer(bos);
jsonRoot.Accept(writer);
} else {
// Use single-ultracompact UTF-8 JSON format
rapidjson::Writer<rapidjson::FileWriteStream> writer(bos);
jsonRoot.Accept(writer);
}
// Close the file.
std::fclose(fp);
}
/*!
Write a bitpit::Config tree contents to json formatted c++ string.
NOTE: JSON root document object does not have a key name unlike XML.
Tree contents will be written to a plain c++ string, with UTF-8 standard encoding, and
appended to any previous content of the source string.
\param[in] source string to write JSON contents.
\param[in] rootConfig pointer to the Config tree to be written on target string
*/
void writeBufferConfiguration(std::string &source, const Config *rootConfig)
{
if (!rootConfig) {
throw std::runtime_error("JSON::writeConfiguration Null Config tree structure passed");
}
// DOM Document is GenericDocument<UTF8<>>
rapidjson::Document jsonRoot;
// Get the allocator
rapidjson::Document::AllocatorType &allocator = jsonRoot.GetAllocator();
// Create a root node and recursively fill it
jsonRoot.SetObject();
writeNode(rootConfig, jsonRoot, allocator);
// Write on a buffer with initial capacity 1024
rapidjson::StringBuffer buffer(0, 1024);
// Use single-ultracompact UTF-8 JSON format
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
jsonRoot.Accept(writer);
source += std::string(buffer.GetString());
}
/*!
Write recursively Config tree contents to JSON objects
The current method visits the config Tree and write Option and nested
sections as internal members of JSON object. MultiSections will be
treated as array of JSON objects.
The method can be used directly on the root content of the JSON document,
specifying as key an empty string.
\param[in] config pointer to local Config tree section to be parsed.
\param[in] rootJSONdata, reference JSON object value to fill
\param[in] allocator Allocator of reference rapidjson Document
*/
void writeNode(const Config *config, rapidjson::Value &rootJSONData, rapidjson::Document::AllocatorType &allocator)
{
// Write the options
for (auto &entry : config->getOptions()) {
const std::string &configKey = entry.first;
const std::string &configValue = entry.second;
rapidjson::Value jsonKey;
jsonKey.SetString(configKey, allocator);
rapidjson::Value jsonValue = encodeValue(configValue, allocator);
rootJSONData.AddMember(jsonKey, jsonValue, allocator);
}
// Get list of unique section keys
std::set<std::string> listOfSectionsKeys;
for (const auto &entry : config->getSections()) {
listOfSectionsKeys.insert(entry.first);
}
// Write the sections
for (const std::string &configKey : listOfSectionsKeys) {
const Config::ConstMultiSection &keySections = config->getSections(configKey);
if (keySections.empty()) {
continue;
}
std::size_t nEntries = keySections.size();
rapidjson::Value object(rapidjson::kObjectType);
rapidjson::Value arrayOfObjects(rapidjson::kArrayType);
if (nEntries > 1) {
arrayOfObjects.Reserve(nEntries, allocator);
}
// Process section content
for (std::size_t i = 0; i< nEntries; ++i) {
writeNode(keySections[i], object, allocator);
if (nEntries > 1) {
arrayOfObjects.PushBack(object, allocator);
rapidjson::Value swapDummy(rapidjson::kObjectType);
object.Swap(swapDummy);
}
}
rapidjson::Value jsonKey;
jsonKey.SetString(configKey, allocator);
if (nEntries > 1) {
rootJSONData.AddMember(jsonKey, arrayOfObjects, allocator);
} else {
rootJSONData.AddMember(jsonKey, object, allocator);
}
}
}
/*!
Convert a JSON value into a string.
Supported JSON values are:
- bool
- number (int/int64, uint/uint64 or float/double)
- string
- null
Objects and array structures are not stringfyied. Unsupported values and
null will be converted in an empty string.
\param[in] value is the JSON value
\result The string corresponding to the specified JSON value.
*/
std::string decodeValue(const rapidjson::Value &value)
{
if (value.IsBool()) {
return std::to_string(int(value.GetBool()));
} else if (value.IsString()) {
return std::string(value.GetString());
} else if (value.IsNumber()) {
// To avoid problems, numbers are always casted to the maximum allowed
// datatype.
std::ostringstream stringStream;
if (value.IsUint64()) {
// Try uint casting first
stringStream << value.GetUint64();
} else if (value.IsInt64()) {
// Then the normal int casting
stringStream << value.GetInt64();
} else {
// Otherwise try it as double
stringStream << std::scientific << std::setprecision(8) << value.GetDouble();
}
return stringStream.str();
} else {
return std::string("");
}
}
/*!
Convert a string into a JSON.
Supported types are:
- unsigned long int (e.g., 123456789);
- signed long int (e.g. -123456789);
- double (+/-123456789.123456789 or in scientific notation);
- null (emtpy strings).
Strings that does't match any know data types are converted to JSON strings.
\param[in] stringValue is the string that will be converted
\param[in] allocator is the JSON allocator
\result The JSON value corresponding to the specified string.
*/
rapidjson::Value encodeValue(const std::string &stringValue, rapidjson::Document::AllocatorType &allocator)
{
rapidjson::Value value;
// Empty string are null values
if (stringValue.empty()) {
return value;
}
// Strings that contains non-numeric charactes are strings
std::size_t nonNumericCharacterPos = stringValue.find_first_not_of("Ee+-.0123456789");
if (nonNumericCharacterPos != std::string::npos) {
value.SetString(stringValue, allocator);
return value;
}
// Convert the string into a number
std::istringstream stringStream(stringValue);
if (stringValue.find_first_of("Ee.") != std::string::npos) {
// Convert the string into a double
double number;
stringStream >> number;
value.SetDouble(number);
} else if (stringValue.find_first_of("-") != std::string::npos) {
// Convert the string into a long
int64_t number;
stringStream >> number;
value.SetInt64(number);
} else {
// Convert the string into an unsigned long
uint64_t number;
stringStream >> number;
value.SetUint64(number);
}
return value;
}
}
}
}
#endif