@@ -86,9 +86,11 @@ static int
8686parseAndSaveValueTree ( const std::string &input,
8787 const std::string &actual,
8888 const std::string &kind,
89- Json::Value &root )
89+ Json::Value &root,
90+ const Json::Features &features,
91+ bool parseOnly )
9092{
91- Json::Reader reader;
93+ Json::Reader reader ( features ) ;
9294 bool parsingSuccessful = reader.parse ( input, root );
9395 if ( !parsingSuccessful )
9496 {
@@ -98,14 +100,17 @@ parseAndSaveValueTree( const std::string &input,
98100 return 1 ;
99101 }
100102
101- FILE *factual = fopen ( actual.c_str (), " wt" );
102- if ( !factual )
103+ if ( !parseOnly )
103104 {
104- printf ( " Failed to create %s actual file.\n " , kind.c_str () );
105- return 2 ;
105+ FILE *factual = fopen ( actual.c_str (), " wt" );
106+ if ( !factual )
107+ {
108+ printf ( " Failed to create %s actual file.\n " , kind.c_str () );
109+ return 2 ;
110+ }
111+ printValueTree ( factual, root );
112+ fclose ( factual );
106113 }
107- printValueTree ( factual, root );
108- fclose ( factual );
109114 return 0 ;
110115}
111116
@@ -143,25 +148,65 @@ removeSuffix( const std::string &path,
143148 return path.substr ( 0 , path.length () - extension.length () );
144149}
145150
151+ static int
152+ printUsage ( const char *argv[] )
153+ {
154+ printf ( " Usage: %s [--strict] input-json-file" , argv[0 ] );
155+ return 3 ;
156+ }
157+
158+
159+ int
160+ parseCommandLine ( int argc, const char *argv[],
161+ Json::Features &features, std::string &path,
162+ bool &parseOnly )
163+ {
164+ parseOnly = false ;
165+ if ( argc < 2 )
166+ {
167+ return printUsage ( argv );
168+ }
169+
170+ int index = 1 ;
171+ if ( std::string (argv[1 ]) == " --json-checker" )
172+ {
173+ features = Json::Features::strictMode ();
174+ parseOnly = true ;
175+ ++index;
176+ }
177+
178+ if ( index == argc || index + 1 < argc )
179+ {
180+ return printUsage ( argv );
181+ }
182+
183+ path = argv[index];
184+ return 0 ;
185+ }
186+
187+
146188int main ( int argc, const char *argv[] )
147189{
148- if ( argc != 2 )
190+ std::string path;
191+ Json::Features features;
192+ bool parseOnly;
193+ int exitCode = parseCommandLine ( argc, argv, features, path, parseOnly );
194+ if ( exitCode != 0 )
149195 {
150- printf ( " Usage: %s input-json-file" , argv[0 ] );
151- return 3 ;
196+ return exitCode;
152197 }
153198
154- std::string input = readInputTestFile ( argv[ 1 ] );
199+ std::string input = readInputTestFile ( path. c_str () );
155200 if ( input.empty () )
156201 {
157- printf ( " Failed to read input or empty input: %s\n " , argv[ 1 ] );
202+ printf ( " Failed to read input or empty input: %s\n " , path. c_str () );
158203 return 3 ;
159204 }
160205
161206 std::string basePath = removeSuffix ( argv[1 ], " .json" );
162- if ( basePath.empty () )
207+ if ( !parseOnly && basePath.empty () )
163208 {
164- printf ( " Bad input path. Path does not end with '.expected':\n %s\n " , argv[ 1 ] );
209+ printf ( " Bad input path. Path does not end with '.expected':\n %s\n " , path. c_str () );
165210 return 3 ;
166211 }
167212
@@ -170,15 +215,16 @@ int main( int argc, const char *argv[] )
170215 std::string rewriteActualPath = basePath + " .actual-rewrite" ;
171216
172217 Json::Value root;
173- int exitCode = parseAndSaveValueTree ( input, actualPath, " input" , root );
174- if ( exitCode == 0 )
218+ exitCode = parseAndSaveValueTree ( input, actualPath, " input" , root, features, parseOnly );
219+ if ( exitCode == 0 && !parseOnly )
175220 {
176221 std::string rewrite;
177222 exitCode = rewriteValueTree ( rewritePath, root, rewrite );
178223 if ( exitCode == 0 )
179224 {
180225 Json::Value rewriteRoot;
181- exitCode = parseAndSaveValueTree ( rewrite, rewriteActualPath, " rewrite" , rewriteRoot );
226+ exitCode = parseAndSaveValueTree ( rewrite, rewriteActualPath,
227+ " rewrite" , rewriteRoot, features, parseOnly );
182228 }
183229 }
184230
0 commit comments