-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_json.cc
276 lines (241 loc) · 8.87 KB
/
param_json.cc
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
/*
* param_json.cc
*
* Created on: March 27, 2015
* Author: nsoblath, bhlaroque
*/
#define SCARAB_API_EXPORTS
#include <sstream>
using std::string;
using std::stringstream;
#include "param_json.hh"
#include "logger.hh"
#include "rapidjson/document.h"
#include "rapidjson/filereadstream.h"
#include "rapidjson/filewritestream.h"
#include "rapidjson/stringbuffer.h"
namespace scarab
{
LOGGER( dlog, "param" );
REGISTER_PARAM_INPUT_CODEC( param_input_json, "json" );
param_input_json::param_input_json()
{
}
param_input_json::~param_input_json()
{
}
param_ptr_t param_input_json::read_file( const std::string& a_filename, const param_node& )
{
FILE* t_config_file = fopen( a_filename.c_str(), "r" );
if( t_config_file == NULL )
{
LERROR( dlog, "file <" << a_filename << "> did not open" );
return NULL;
}
char t_buffer[ RAPIDJSON_FILE_BUFFER_SIZE ];
rapidjson::FileReadStream t_file_stream( t_config_file, t_buffer, sizeof(t_buffer) );
rapidjson::Document t_config_doc;
if( t_config_doc.ParseStream<0>( t_file_stream ).HasParseError() )
{
unsigned errorPos = t_config_doc.GetErrorOffset();
rewind( t_config_file );
unsigned iChar, newlineCount = 1, lastNewlinePos = 0;
int thisChar;
for( iChar = 0; iChar != errorPos; ++iChar )
{
thisChar = fgetc( t_config_file );
if( thisChar == EOF )
{
break;
}
if( thisChar == '\n' || thisChar == '\r' )
{
newlineCount++;
lastNewlinePos = iChar + 1;
}
}
if( iChar == errorPos )
{
LERROR( dlog, "error parsing config file :\n" <<
'\t' << t_config_doc.GetParseError() << '\n' <<
"\tThe error was reported at line " << newlineCount << ", character " << errorPos - lastNewlinePos );
}
else
{
LERROR( dlog, "error parsing config file :\n" <<
'\t' << t_config_doc.GetParseError() <<
"\tend of file reached before error location was found" );
}
fclose( t_config_file );
return NULL;
}
fclose( t_config_file );
return param_input_json::read_document( t_config_doc );
}
param_ptr_t param_input_json::read_string( const std::string& a_json_string, const param_node& )
{
rapidjson::Document t_config_doc;
if( t_config_doc.Parse<0>( a_json_string.c_str() ).HasParseError() )
{
LERROR( dlog, "error parsing string:\n" << t_config_doc.GetParseError() );
return NULL;
}
return param_input_json::read_document( t_config_doc );
}
param_ptr_t param_input_json::read_document( const rapidjson::Document& a_doc )
{
return read_value( a_doc );
}
param_ptr_t param_input_json::read_value( const rapidjson::Value& a_value )
{
if( a_value.IsNull() )
{
return std::unique_ptr< param >( new param() );
}
if( a_value.IsObject() )
{
std::unique_ptr< param_node > t_obj_as_param( new param_node() );
for( rapidjson::Value::ConstMemberIterator jsonIt = a_value.MemberBegin();
jsonIt != a_value.MemberEnd();
++jsonIt)
{
t_obj_as_param->replace( jsonIt->name.GetString(), param_input_json::read_value( jsonIt->value ) );
}
return t_obj_as_param;
}
if( a_value.IsArray() )
{
std::unique_ptr< param_array > t_array_as_param( new param_array() );
for( rapidjson::Value::ConstValueIterator jsonIt = a_value.Begin();
jsonIt != a_value.End();
++jsonIt)
{
t_array_as_param->push_back( std::move(*param_input_json::read_value( *jsonIt )) );
}
return t_array_as_param;
}
if( a_value.IsString() )
{
//LWARN( dlog, "reading string from json: " << a_value.GetString() );
return std::unique_ptr< param_value >( new param_value( a_value.GetString() ) );
}
if( a_value.IsBool() )
{
//LWARN( dlog, "reading bool from json: " << a_value.GetBool() );
return std::unique_ptr< param_value >( new param_value( a_value.GetBool() ) );
}
if( a_value.IsInt() )
{
//LWARN( dlog, "reading int from json: " << a_value.GetInt() );
return std::unique_ptr< param_value >( new param_value( a_value.GetInt() ) ) ;
}
if( a_value.IsUint() )
{
//LWARN( dlog, "reading uint from json: " << a_value.GetUint() );
return std::unique_ptr< param_value >( new param_value( a_value.GetUint() ) );
}
if( a_value.IsInt64() )
{
//LWARN( dlog, "reading int64 from json: " << a_value.GetInt64() );
return std::unique_ptr< param_value >( new param_value( a_value.GetInt64() ) );
}
if( a_value.IsUint64() )
{
//LWARN( dlog, "reading uint64 from json: " << a_value.GetUint64() );
return std::unique_ptr< param_value >( new param_value( a_value.GetUint64() ) );
}
if( a_value.IsDouble() )
{
//LWARN( dlog, "reading double from json: " << a_value.GetDouble() );
return std::unique_ptr< param_value >( new param_value( a_value.GetDouble() ) );
}
LWARN( dlog, "(config_reader_json) unknown type; returning null value" );
return std::unique_ptr< param >( new param() );
}
REGISTER_PARAM_OUTPUT_CODEC( param_output_json, "json" );
param_output_json::param_output_json()
{}
param_output_json::~param_output_json()
{}
bool param_output_json::write_file( const param& a_to_write, const std::string& a_filename, const param_node& a_options )
{
if( a_filename.empty() )
{
LERROR( dlog, "Filename cannot be an empty string" );
return false;
}
FILE* file = fopen( a_filename.c_str(), "w" );
if( file == NULL )
{
LERROR( dlog, "Unable to open file: " << a_filename );
return false;
}
char t_buffer[ RAPIDJSON_FILE_BUFFER_SIZE ];
rapidjson::FileWriteStream t_filestream( file, t_buffer, sizeof(t_buffer) );
json_writing_style t_style = k_compact;
if( a_options.has( "style" ) )
{
if( a_options["style"]().is_uint() )
{
t_style = (json_writing_style)a_options.get_value< unsigned >( "style", k_compact );
}
else
{
string t_style_string( a_options.get_value( "style", "compact" ) );
if( t_style_string == string( "pretty" ) ) t_style = k_pretty;
}
}
bool t_result = false;
if( t_style == k_compact )
{
rj_file_writer t_writer( t_filestream );
t_result = param_output_json::write_param( a_to_write, &t_writer );
}
else
{
rj_pretty_file_writer t_writer( t_filestream );
t_result = param_output_json::write_param( a_to_write, &t_writer );
}
if (! t_result )
{
LERROR( dlog, "Error while writing file" );
return false;
}
return true;
}
bool param_output_json::write_string( const param& a_to_write, std::string& a_string, const param_node& a_options )
{
rapidjson::StringBuffer t_str_buff;
json_writing_style t_style = k_compact;
if( a_options.has( "style" ) )
{
if( a_options["style"]().is_uint() )
{
t_style = (json_writing_style)a_options.get_value< unsigned >( "style", k_compact );
}
else
{
string t_style_string( a_options.get_value( "style", "compact" ) );
if( t_style_string == string( "pretty" ) ) t_style = k_pretty;
}
}
bool t_result = false;
if( t_style == k_compact )
{
rj_string_writer t_writer( t_str_buff );
t_result = param_output_json::write_param( a_to_write, &t_writer );
}
else
{
rj_pretty_string_writer t_writer( t_str_buff );
t_result = param_output_json::write_param( a_to_write, &t_writer );
}
if (! t_result )
{
LERROR( dlog, "Error while writing string" );
return false;
}
a_string.assign( t_str_buff.GetString() );
return true;
}
} /* namespace scarab */