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