blob: 231ee0c44d06c6d1c07eff0337691e54cbfef90a [file] [log] [blame]
Christopher Dunnf9864232007-06-14 21:01:26 +00001#include <json/json.h>
2#include <algorithm> // sort
3#include <stdio.h>
4
5#if defined(_MSC_VER) && _MSC_VER >= 1310
6# pragma warning( disable: 4996 ) // disable fopen deprecation warning
7#endif
8
9static std::string
10readInputTestFile( const char *path )
11{
12 FILE *file = fopen( path, "rb" );
13 if ( !file )
14 return std::string("");
15 fseek( file, 0, SEEK_END );
16 long size = ftell( file );
17 fseek( file, 0, SEEK_SET );
18 std::string text;
19 char *buffer = new char[size+1];
20 buffer[size] = 0;
21 if ( fread( buffer, 1, size, file ) == (unsigned long)size )
22 text = buffer;
23 fclose( file );
24 delete[] buffer;
25 return text;
26}
27
28
29static void
30printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." )
31{
32 switch ( value.type() )
33 {
34 case Json::nullValue:
35 fprintf( fout, "%s=null\n", path.c_str() );
36 break;
37 case Json::intValue:
38 fprintf( fout, "%s=%d\n", path.c_str(), value.asInt() );
39 break;
40 case Json::uintValue:
41 fprintf( fout, "%s=%u\n", path.c_str(), value.asUInt() );
42 break;
43 case Json::realValue:
44 fprintf( fout, "%s=%.16g\n", path.c_str(), value.asDouble() );
45 break;
46 case Json::stringValue:
47 fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() );
48 break;
49 case Json::booleanValue:
50 fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" );
51 break;
52 case Json::arrayValue:
53 {
54 fprintf( fout, "%s=[]\n", path.c_str() );
55 int size = value.size();
56 for ( int index =0; index < size; ++index )
57 {
58 static char buffer[16];
59 sprintf( buffer, "[%d]", index );
60 printValueTree( fout, value[index], path + buffer );
61 }
62 }
63 break;
64 case Json::objectValue:
65 {
66 fprintf( fout, "%s={}\n", path.c_str() );
67 Json::Value::Members members( value.getMemberNames() );
68 std::sort( members.begin(), members.end() );
69 std::string suffix = *(path.end()-1) == '.' ? "" : ".";
70 for ( Json::Value::Members::iterator it = members.begin();
71 it != members.end();
72 ++it )
73 {
74 const std::string &name = *it;
75 printValueTree( fout, value[name], path + suffix + name );
76 }
77 }
78 break;
79 default:
80 break;
81 }
82}
83
84
85static int
86parseAndSaveValueTree( const std::string &input,
87 const std::string &actual,
88 const std::string &kind,
Baptiste Lepilleur88681472009-11-18 21:38:54 +000089 Json::Value &root,
90 const Json::Features &features,
91 bool parseOnly )
Christopher Dunnf9864232007-06-14 21:01:26 +000092{
Baptiste Lepilleur88681472009-11-18 21:38:54 +000093 Json::Reader reader( features );
Christopher Dunnf9864232007-06-14 21:01:26 +000094 bool parsingSuccessful = reader.parse( input, root );
95 if ( !parsingSuccessful )
96 {
97 printf( "Failed to parse %s file: \n%s\n",
98 kind.c_str(),
99 reader.getFormatedErrorMessages().c_str() );
100 return 1;
101 }
102
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000103 if ( !parseOnly )
Christopher Dunnf9864232007-06-14 21:01:26 +0000104 {
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000105 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 );
Christopher Dunnf9864232007-06-14 21:01:26 +0000113 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000114 return 0;
115}
116
117
118static int
119rewriteValueTree( const std::string &rewritePath,
120 const Json::Value &root,
121 std::string &rewrite )
122{
123 //Json::FastWriter writer;
124 //writer.enableYAMLCompatibility();
125 Json::StyledWriter writer;
126 rewrite = writer.write( root );
127 FILE *fout = fopen( rewritePath.c_str(), "wt" );
128 if ( !fout )
129 {
130 printf( "Failed to create rewrite file: %s\n", rewritePath.c_str() );
131 return 2;
132 }
133 fprintf( fout, "%s\n", rewrite.c_str() );
134 fclose( fout );
135 return 0;
136}
137
138
139static std::string
140removeSuffix( const std::string &path,
141 const std::string &extension )
142{
143 if ( extension.length() >= path.length() )
144 return std::string("");
145 std::string suffix = path.substr( path.length() - extension.length() );
146 if ( suffix != extension )
147 return std::string("");
148 return path.substr( 0, path.length() - extension.length() );
149}
150
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000151static int
152printUsage( const char *argv[] )
Christopher Dunnf9864232007-06-14 21:01:26 +0000153{
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000154 printf( "Usage: %s [--strict] input-json-file", argv[0] );
155 return 3;
156}
157
158
159int
160parseCommandLine( int argc, const char *argv[],
161 Json::Features &features, std::string &path,
162 bool &parseOnly )
163{
164 parseOnly = false;
165 if ( argc < 2 )
Christopher Dunnf9864232007-06-14 21:01:26 +0000166 {
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000167 return printUsage( argv );
Christopher Dunnf9864232007-06-14 21:01:26 +0000168 }
169
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000170 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
188int main( int argc, const char *argv[] )
189{
190 std::string path;
191 Json::Features features;
192 bool parseOnly;
193 int exitCode = parseCommandLine( argc, argv, features, path, parseOnly );
194 if ( exitCode != 0 )
195 {
196 return exitCode;
197 }
198
199 std::string input = readInputTestFile( path.c_str() );
Christopher Dunnf9864232007-06-14 21:01:26 +0000200 if ( input.empty() )
201 {
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000202 printf( "Failed to read input or empty input: %s\n", path.c_str() );
Christopher Dunnf9864232007-06-14 21:01:26 +0000203 return 3;
204 }
205
206 std::string basePath = removeSuffix( argv[1], ".json" );
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000207 if ( !parseOnly && basePath.empty() )
Christopher Dunnf9864232007-06-14 21:01:26 +0000208 {
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000209 printf( "Bad input path. Path does not end with '.expected':\n%s\n", path.c_str() );
Christopher Dunnf9864232007-06-14 21:01:26 +0000210 return 3;
211 }
212
213 std::string actualPath = basePath + ".actual";
214 std::string rewritePath = basePath + ".rewrite";
215 std::string rewriteActualPath = basePath + ".actual-rewrite";
216
217 Json::Value root;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000218 exitCode = parseAndSaveValueTree( input, actualPath, "input", root, features, parseOnly );
219 if ( exitCode == 0 && !parseOnly )
Christopher Dunnf9864232007-06-14 21:01:26 +0000220 {
221 std::string rewrite;
222 exitCode = rewriteValueTree( rewritePath, root, rewrite );
223 if ( exitCode == 0 )
224 {
225 Json::Value rewriteRoot;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000226 exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath,
227 "rewrite", rewriteRoot, features, parseOnly );
Christopher Dunnf9864232007-06-14 21:01:26 +0000228 }
229 }
230
231 return exitCode;
232}
233