Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur |
| 2 | // Distributed under MIT license, or public domain if desired and |
| 3 | // recognized in your jurisdiction. |
| 4 | // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE |
| 5 | |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 6 | /* This executable is used for testing parser/writer using real JSON files. |
| 7 | */ |
| 8 | |
| 9 | |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 10 | #include <json/json.h> |
| 11 | #include <algorithm> // sort |
| 12 | #include <stdio.h> |
| 13 | |
| 14 | #if defined(_MSC_VER) && _MSC_VER >= 1310 |
| 15 | # pragma warning( disable: 4996 ) // disable fopen deprecation warning |
| 16 | #endif |
| 17 | |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 18 | static std::string |
| 19 | normalizeFloatingPointStr( double value ) |
| 20 | { |
| 21 | char buffer[32]; |
Aaron Jacobs | d261880 | 2013-08-08 23:08:28 +0000 | [diff] [blame] | 22 | #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) |
| 23 | sprintf_s( buffer, sizeof(buffer), "%.16g", value ); |
| 24 | #else |
Aaron Jacobs | 36400ac | 2013-08-08 00:39:32 +0000 | [diff] [blame] | 25 | snprintf( buffer, sizeof(buffer), "%.16g", value ); |
Aaron Jacobs | d261880 | 2013-08-08 23:08:28 +0000 | [diff] [blame] | 26 | #endif |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 27 | buffer[sizeof(buffer)-1] = 0; |
| 28 | std::string s( buffer ); |
| 29 | std::string::size_type index = s.find_last_of( "eE" ); |
| 30 | if ( index != std::string::npos ) |
| 31 | { |
| 32 | std::string::size_type hasSign = (s[index+1] == '+' || s[index+1] == '-') ? 1 : 0; |
| 33 | std::string::size_type exponentStartIndex = index + 1 + hasSign; |
| 34 | std::string normalized = s.substr( 0, exponentStartIndex ); |
| 35 | std::string::size_type indexDigit = s.find_first_not_of( '0', exponentStartIndex ); |
| 36 | std::string exponent = "0"; |
| 37 | if ( indexDigit != std::string::npos ) // There is an exponent different from 0 |
| 38 | { |
| 39 | exponent = s.substr( indexDigit ); |
| 40 | } |
| 41 | return normalized + exponent; |
| 42 | } |
| 43 | return s; |
| 44 | } |
| 45 | |
| 46 | |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 47 | static std::string |
| 48 | readInputTestFile( const char *path ) |
| 49 | { |
| 50 | FILE *file = fopen( path, "rb" ); |
| 51 | if ( !file ) |
| 52 | return std::string(""); |
| 53 | fseek( file, 0, SEEK_END ); |
| 54 | long size = ftell( file ); |
| 55 | fseek( file, 0, SEEK_SET ); |
| 56 | std::string text; |
| 57 | char *buffer = new char[size+1]; |
| 58 | buffer[size] = 0; |
| 59 | if ( fread( buffer, 1, size, file ) == (unsigned long)size ) |
| 60 | text = buffer; |
| 61 | fclose( file ); |
| 62 | delete[] buffer; |
| 63 | return text; |
| 64 | } |
| 65 | |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 66 | static void |
| 67 | printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." ) |
| 68 | { |
| 69 | switch ( value.type() ) |
| 70 | { |
| 71 | case Json::nullValue: |
| 72 | fprintf( fout, "%s=null\n", path.c_str() ); |
| 73 | break; |
| 74 | case Json::intValue: |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 75 | fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestInt() ).c_str() ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 76 | break; |
| 77 | case Json::uintValue: |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 78 | fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestUInt() ).c_str() ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 79 | break; |
| 80 | case Json::realValue: |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 81 | fprintf( fout, "%s=%s\n", path.c_str(), normalizeFloatingPointStr(value.asDouble()).c_str() ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 82 | break; |
| 83 | case Json::stringValue: |
| 84 | fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() ); |
| 85 | break; |
| 86 | case Json::booleanValue: |
| 87 | fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" ); |
| 88 | break; |
| 89 | case Json::arrayValue: |
| 90 | { |
| 91 | fprintf( fout, "%s=[]\n", path.c_str() ); |
| 92 | int size = value.size(); |
| 93 | for ( int index =0; index < size; ++index ) |
| 94 | { |
| 95 | static char buffer[16]; |
Aaron Jacobs | d261880 | 2013-08-08 23:08:28 +0000 | [diff] [blame] | 96 | #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) |
| 97 | sprintf_s( buffer, sizeof(buffer), "[%d]", index ); |
| 98 | #else |
Aaron Jacobs | 36400ac | 2013-08-08 00:39:32 +0000 | [diff] [blame] | 99 | snprintf( buffer, sizeof(buffer), "[%d]", index ); |
Aaron Jacobs | d261880 | 2013-08-08 23:08:28 +0000 | [diff] [blame] | 100 | #endif |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 101 | printValueTree( fout, value[index], path + buffer ); |
| 102 | } |
| 103 | } |
| 104 | break; |
| 105 | case Json::objectValue: |
| 106 | { |
| 107 | fprintf( fout, "%s={}\n", path.c_str() ); |
| 108 | Json::Value::Members members( value.getMemberNames() ); |
| 109 | std::sort( members.begin(), members.end() ); |
| 110 | std::string suffix = *(path.end()-1) == '.' ? "" : "."; |
| 111 | for ( Json::Value::Members::iterator it = members.begin(); |
| 112 | it != members.end(); |
| 113 | ++it ) |
| 114 | { |
| 115 | const std::string &name = *it; |
| 116 | printValueTree( fout, value[name], path + suffix + name ); |
| 117 | } |
| 118 | } |
| 119 | break; |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static int |
| 127 | parseAndSaveValueTree( const std::string &input, |
| 128 | const std::string &actual, |
| 129 | const std::string &kind, |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 130 | Json::Value &root, |
| 131 | const Json::Features &features, |
| 132 | bool parseOnly ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 133 | { |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 134 | Json::Reader reader( features ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 135 | bool parsingSuccessful = reader.parse( input, root ); |
| 136 | if ( !parsingSuccessful ) |
| 137 | { |
| 138 | printf( "Failed to parse %s file: \n%s\n", |
| 139 | kind.c_str(), |
Baptiste Lepilleur | b2e8ccc | 2011-05-01 16:27:55 +0000 | [diff] [blame] | 140 | reader.getFormattedErrorMessages().c_str() ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 141 | return 1; |
| 142 | } |
| 143 | |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 144 | if ( !parseOnly ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 145 | { |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 146 | FILE *factual = fopen( actual.c_str(), "wt" ); |
| 147 | if ( !factual ) |
| 148 | { |
| 149 | printf( "Failed to create %s actual file.\n", kind.c_str() ); |
| 150 | return 2; |
| 151 | } |
| 152 | printValueTree( factual, root ); |
| 153 | fclose( factual ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 154 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | static int |
| 160 | rewriteValueTree( const std::string &rewritePath, |
| 161 | const Json::Value &root, |
| 162 | std::string &rewrite ) |
| 163 | { |
| 164 | //Json::FastWriter writer; |
| 165 | //writer.enableYAMLCompatibility(); |
| 166 | Json::StyledWriter writer; |
| 167 | rewrite = writer.write( root ); |
| 168 | FILE *fout = fopen( rewritePath.c_str(), "wt" ); |
| 169 | if ( !fout ) |
| 170 | { |
| 171 | printf( "Failed to create rewrite file: %s\n", rewritePath.c_str() ); |
| 172 | return 2; |
| 173 | } |
| 174 | fprintf( fout, "%s\n", rewrite.c_str() ); |
| 175 | fclose( fout ); |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static std::string |
| 181 | removeSuffix( const std::string &path, |
| 182 | const std::string &extension ) |
| 183 | { |
| 184 | if ( extension.length() >= path.length() ) |
| 185 | return std::string(""); |
| 186 | std::string suffix = path.substr( path.length() - extension.length() ); |
| 187 | if ( suffix != extension ) |
| 188 | return std::string(""); |
| 189 | return path.substr( 0, path.length() - extension.length() ); |
| 190 | } |
| 191 | |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 192 | |
| 193 | static void |
| 194 | printConfig() |
| 195 | { |
| 196 | // Print the configuration used to compile JsonCpp |
| 197 | #if defined(JSON_NO_INT64) |
| 198 | printf( "JSON_NO_INT64=1\n" ); |
| 199 | #else |
| 200 | printf( "JSON_NO_INT64=0\n" ); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 205 | static int |
| 206 | printUsage( const char *argv[] ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 207 | { |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 208 | printf( "Usage: %s [--strict] input-json-file", argv[0] ); |
| 209 | return 3; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | int |
| 214 | parseCommandLine( int argc, const char *argv[], |
| 215 | Json::Features &features, std::string &path, |
| 216 | bool &parseOnly ) |
| 217 | { |
| 218 | parseOnly = false; |
| 219 | if ( argc < 2 ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 220 | { |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 221 | return printUsage( argv ); |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 224 | int index = 1; |
| 225 | if ( std::string(argv[1]) == "--json-checker" ) |
| 226 | { |
| 227 | features = Json::Features::strictMode(); |
| 228 | parseOnly = true; |
| 229 | ++index; |
| 230 | } |
| 231 | |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 232 | if ( std::string(argv[1]) == "--json-config" ) |
| 233 | { |
| 234 | printConfig(); |
| 235 | return 3; |
| 236 | } |
| 237 | |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 238 | if ( index == argc || index + 1 < argc ) |
| 239 | { |
| 240 | return printUsage( argv ); |
| 241 | } |
| 242 | |
| 243 | path = argv[index]; |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | int main( int argc, const char *argv[] ) |
| 249 | { |
| 250 | std::string path; |
| 251 | Json::Features features; |
| 252 | bool parseOnly; |
| 253 | int exitCode = parseCommandLine( argc, argv, features, path, parseOnly ); |
| 254 | if ( exitCode != 0 ) |
| 255 | { |
| 256 | return exitCode; |
| 257 | } |
| 258 | |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 259 | try |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 260 | { |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 261 | std::string input = readInputTestFile( path.c_str() ); |
| 262 | if ( input.empty() ) |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 263 | { |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 264 | printf( "Failed to read input or empty input: %s\n", path.c_str() ); |
| 265 | return 3; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 266 | } |
Baptiste Lepilleur | 842e9ac | 2010-12-27 17:45:23 +0000 | [diff] [blame] | 267 | |
| 268 | std::string basePath = removeSuffix( argv[1], ".json" ); |
| 269 | if ( !parseOnly && basePath.empty() ) |
| 270 | { |
| 271 | printf( "Bad input path. Path does not end with '.expected':\n%s\n", path.c_str() ); |
| 272 | return 3; |
| 273 | } |
| 274 | |
| 275 | std::string actualPath = basePath + ".actual"; |
| 276 | std::string rewritePath = basePath + ".rewrite"; |
| 277 | std::string rewriteActualPath = basePath + ".actual-rewrite"; |
| 278 | |
| 279 | Json::Value root; |
| 280 | exitCode = parseAndSaveValueTree( input, actualPath, "input", root, features, parseOnly ); |
| 281 | if ( exitCode == 0 && !parseOnly ) |
| 282 | { |
| 283 | std::string rewrite; |
| 284 | exitCode = rewriteValueTree( rewritePath, root, rewrite ); |
| 285 | if ( exitCode == 0 ) |
| 286 | { |
| 287 | Json::Value rewriteRoot; |
| 288 | exitCode = parseAndSaveValueTree( rewrite, rewriteActualPath, |
| 289 | "rewrite", rewriteRoot, features, parseOnly ); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | catch ( const std::exception &e ) |
| 294 | { |
| 295 | printf( "Unhandled exception:\n%s\n", e.what() ); |
| 296 | exitCode = 1; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return exitCode; |
| 300 | } |
Christopher Dunn | 77cd838 | 2014-04-19 21:41:03 +0000 | [diff] [blame^] | 301 | // vim: et ts=4 sts=4 sw=4 tw=0 |