16
16
#include < algorithm> // sort
17
17
#include < cstdio>
18
18
#include < json/json.h>
19
+ #include < memory>
19
20
#include < sstream>
20
21
21
22
struct Options {
@@ -126,15 +127,39 @@ static int parseAndSaveValueTree(const Json::String& input,
126
127
const Json::String& actual,
127
128
const Json::String& kind,
128
129
const Json::Features& features, bool parseOnly,
129
- Json::Value* root) {
130
- Json::Reader reader (features);
131
- bool parsingSuccessful =
132
- reader.parse (input.data (), input.data () + input.size (), *root);
133
- if (!parsingSuccessful) {
134
- printf (" Failed to parse %s file: \n %s\n " , kind.c_str (),
135
- reader.getFormattedErrorMessages ().c_str ());
136
- return 1 ;
130
+ Json::Value* root, bool use_legacy) {
131
+ if (!use_legacy) {
132
+ Json::CharReaderBuilder builder;
133
+
134
+ builder.settings_ [" allowComments" ] = features.allowComments_ ;
135
+ builder.settings_ [" strictRoot" ] = features.strictRoot_ ;
136
+ builder.settings_ [" allowDroppedNullPlaceholders" ] =
137
+ features.allowDroppedNullPlaceholders_ ;
138
+ builder.settings_ [" allowNumericKeys" ] = features.allowNumericKeys_ ;
139
+
140
+ std::unique_ptr<Json::CharReader> reader (builder.newCharReader ());
141
+ Json::String errors;
142
+ const bool parsingSuccessful =
143
+ reader->parse (input.data (), input.data () + input.size (), root, &errors);
144
+
145
+ if (!parsingSuccessful) {
146
+ printf (" Failed to parse %s file: \n %s\n " , kind.c_str (), errors.c_str ());
147
+ return 1 ;
148
+ }
149
+
150
+ // We may instead check the legacy implementation (to ensure it doesn't
151
+ // randomly get broken).
152
+ } else {
153
+ Json::Reader reader (features);
154
+ const bool parsingSuccessful =
155
+ reader.parse (input.data (), input.data () + input.size (), *root);
156
+ if (!parsingSuccessful) {
157
+ printf (" Failed to parse %s file: \n %s\n " , kind.c_str (),
158
+ reader.getFormattedErrorMessages ().c_str ());
159
+ return 1 ;
160
+ }
137
161
}
162
+
138
163
if (!parseOnly) {
139
164
FILE* factual = fopen (actual.c_str (), " wt" );
140
165
if (!factual) {
@@ -240,7 +265,8 @@ static int parseCommandLine(int argc, const char* argv[], Options* opts) {
240
265
opts->path = argv[index];
241
266
return 0 ;
242
267
}
243
- static int runTest (Options const & opts) {
268
+
269
+ static int runTest (Options const & opts, bool use_legacy) {
244
270
int exitCode = 0 ;
245
271
246
272
Json::String input = readInputTestFile (opts.path .c_str ());
@@ -262,23 +288,25 @@ static int runTest(Options const& opts) {
262
288
263
289
Json::Value root;
264
290
exitCode = parseAndSaveValueTree (input, actualPath, " input" , opts.features ,
265
- opts.parseOnly , &root);
291
+ opts.parseOnly , &root, use_legacy );
266
292
if (exitCode || opts.parseOnly ) {
267
293
return exitCode;
268
294
}
295
+
269
296
Json::String rewrite;
270
297
exitCode = rewriteValueTree (rewritePath, root, opts.write , &rewrite);
271
298
if (exitCode) {
272
299
return exitCode;
273
300
}
301
+
274
302
Json::Value rewriteRoot;
275
303
exitCode = parseAndSaveValueTree (rewrite, rewriteActualPath, " rewrite" ,
276
- opts.features , opts.parseOnly , &rewriteRoot);
277
- if (exitCode) {
278
- return exitCode;
279
- }
280
- return 0 ;
304
+ opts.features , opts.parseOnly , &rewriteRoot,
305
+ use_legacy);
306
+
307
+ return exitCode;
281
308
}
309
+
282
310
int main (int argc, const char * argv[]) {
283
311
Options opts;
284
312
try {
@@ -287,7 +315,16 @@ int main(int argc, const char* argv[]) {
287
315
printf (" Failed to parse command-line." );
288
316
return exitCode;
289
317
}
290
- return runTest (opts);
318
+
319
+ // TODO(baylesj): replace this with proper calls to both. Right now
320
+ // we only check the legacy if the modern one is not broken.
321
+ const int modern_return_code = runTest (opts, false );
322
+ const int legacy_return_code = runTest (opts, true );
323
+ if (modern_return_code) {
324
+ return modern_return_code;
325
+ } else {
326
+ return legacy_return_code;
327
+ }
291
328
} catch (const std::exception& e) {
292
329
printf (" Unhandled exception:\n %s\n " , e.what ());
293
330
return 1 ;
0 commit comments