Devin Jeanpierre | 59e4d35 | 2017-07-21 03:44:36 -0700 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Baptiste Lepilleur | 7469f1d | 2010-04-20 21:35:19 +0000 | [diff] [blame] | 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 | |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 6 | #if defined(__GNUC__) |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 7 | #pragma GCC diagnostic push |
| 8 | #pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 9 | #elif defined(_MSC_VER) |
| 10 | #pragma warning(disable : 4996) |
| 11 | #endif |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 12 | |
Baptiste Lepilleur | fa130ef | 2010-12-24 12:47:14 +0000 | [diff] [blame] | 13 | /* This executable is used for testing parser/writer using real JSON files. |
| 14 | */ |
| 15 | |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 16 | #include <algorithm> // sort |
Billy Donahue | dc4a7f9 | 2019-01-17 11:07:53 -0500 | [diff] [blame] | 17 | #include <cstdio> |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 18 | #include <iostream> |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 19 | #include <json/json.h> |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 20 | #include <memory> |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 21 | #include <sstream> |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 22 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 23 | struct Options { |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 24 | Json::String path; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 25 | Json::Features features; |
| 26 | bool parseOnly; |
Billy Donahue | 2b593a9 | 2019-01-18 03:46:57 -0500 | [diff] [blame] | 27 | using writeFuncType = Json::String (*)(Json::Value const&); |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 28 | writeFuncType write; |
| 29 | }; |
| 30 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 31 | static Json::String normalizeFloatingPointStr(double value) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 32 | char buffer[32]; |
Hans Johnson | 5c8e539 | 2018-12-12 13:31:55 -0600 | [diff] [blame] | 33 | jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 34 | buffer[sizeof(buffer) - 1] = 0; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 35 | Json::String s(buffer); |
| 36 | Json::String::size_type index = s.find_last_of("eE"); |
| 37 | if (index != Json::String::npos) { |
| 38 | Json::String::size_type hasSign = |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 39 | (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 40 | Json::String::size_type exponentStartIndex = index + 1 + hasSign; |
| 41 | Json::String normalized = s.substr(0, exponentStartIndex); |
Billy Donahue | 2b593a9 | 2019-01-18 03:46:57 -0500 | [diff] [blame] | 42 | Json::String::size_type indexDigit = |
| 43 | s.find_first_not_of('0', exponentStartIndex); |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 44 | Json::String exponent = "0"; |
| 45 | if (indexDigit != Json::String::npos) // There is an exponent different |
Billy Donahue | 2b593a9 | 2019-01-18 03:46:57 -0500 | [diff] [blame] | 46 | // from 0 |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 47 | { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | exponent = s.substr(indexDigit); |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 49 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 50 | return normalized + exponent; |
| 51 | } |
| 52 | return s; |
Baptiste Lepilleur | f0b24e7 | 2011-05-26 20:14:32 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 55 | static Json::String readInputTestFile(const char* path) { |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 56 | FILE* file = fopen(path, "rb"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 57 | if (!file) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 58 | return ""; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 59 | fseek(file, 0, SEEK_END); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 60 | long const size = ftell(file); |
Hans Johnson | 1fc3de7 | 2019-01-14 17:09:15 -0600 | [diff] [blame] | 61 | size_t const usize = static_cast<unsigned long>(size); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 62 | fseek(file, 0, SEEK_SET); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 63 | char* buffer = new char[size + 1]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 64 | buffer[size] = 0; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 65 | Json::String text; |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 66 | if (fread(buffer, 1, usize, file) == usize) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 67 | text = buffer; |
| 68 | fclose(file); |
| 69 | delete[] buffer; |
| 70 | return text; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 73 | static void printValueTree(FILE* fout, Json::Value& value, |
| 74 | const Json::String& path = ".") { |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 75 | if (value.hasComment(Json::commentBefore)) { |
| 76 | fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str()); |
| 77 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 78 | switch (value.type()) { |
| 79 | case Json::nullValue: |
| 80 | fprintf(fout, "%s=null\n", path.c_str()); |
| 81 | break; |
| 82 | case Json::intValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 83 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 84 | Json::valueToString(value.asLargestInt()).c_str()); |
| 85 | break; |
| 86 | case Json::uintValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 87 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 88 | Json::valueToString(value.asLargestUInt()).c_str()); |
| 89 | break; |
| 90 | case Json::realValue: |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 91 | fprintf(fout, "%s=%s\n", path.c_str(), |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 92 | normalizeFloatingPointStr(value.asDouble()).c_str()); |
| 93 | break; |
| 94 | case Json::stringValue: |
| 95 | fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str()); |
| 96 | break; |
| 97 | case Json::booleanValue: |
| 98 | fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false"); |
| 99 | break; |
| 100 | case Json::arrayValue: { |
| 101 | fprintf(fout, "%s=[]\n", path.c_str()); |
Christopher Dunn | d4513fc | 2016-02-06 09:25:20 -0600 | [diff] [blame] | 102 | Json::ArrayIndex size = value.size(); |
| 103 | for (Json::ArrayIndex index = 0; index < size; ++index) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 104 | static char buffer[16]; |
Hans Johnson | 5c8e539 | 2018-12-12 13:31:55 -0600 | [diff] [blame] | 105 | jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index); |
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()); |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 113 | Json::String suffix = *(path.end() - 1) == '.' ? "" : "."; |
Hans Johnson | cbeed7b | 2019-01-14 17:09:12 -0600 | [diff] [blame] | 114 | for (auto name : members) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 115 | printValueTree(fout, value[name], path + suffix + name); |
| 116 | } |
| 117 | } break; |
| 118 | default: |
| 119 | break; |
| 120 | } |
Cory Quammen | 4d23492 | 2014-10-09 16:29:47 -0400 | [diff] [blame] | 121 | |
| 122 | if (value.hasComment(Json::commentAfter)) { |
| 123 | fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str()); |
| 124 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 127 | static int parseAndSaveValueTree(const Json::String& input, |
| 128 | const Json::String& actual, |
| 129 | const Json::String& kind, |
Jordan Bayles | f34bf24 | 2019-10-11 11:19:00 -0700 | [diff] [blame] | 130 | const Json::Features& features, bool parseOnly, |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 131 | Json::Value* root, bool use_legacy) { |
| 132 | if (!use_legacy) { |
| 133 | Json::CharReaderBuilder builder; |
| 134 | |
| 135 | builder.settings_["allowComments"] = features.allowComments_; |
| 136 | builder.settings_["strictRoot"] = features.strictRoot_; |
| 137 | builder.settings_["allowDroppedNullPlaceholders"] = |
| 138 | features.allowDroppedNullPlaceholders_; |
| 139 | builder.settings_["allowNumericKeys"] = features.allowNumericKeys_; |
| 140 | |
| 141 | std::unique_ptr<Json::CharReader> reader(builder.newCharReader()); |
| 142 | Json::String errors; |
| 143 | const bool parsingSuccessful = |
| 144 | reader->parse(input.data(), input.data() + input.size(), root, &errors); |
| 145 | |
| 146 | if (!parsingSuccessful) { |
| 147 | std::cerr << "Failed to parse " << kind << " file: " << std::endl |
| 148 | << errors << std::endl; |
| 149 | return 1; |
| 150 | } |
| 151 | |
| 152 | // We may instead check the legacy implementation (to ensure it doesn't |
| 153 | // randomly get broken). |
| 154 | } else { |
| 155 | Json::Reader reader(features); |
| 156 | const bool parsingSuccessful = |
| 157 | reader.parse(input.data(), input.data() + input.size(), *root); |
| 158 | if (!parsingSuccessful) { |
| 159 | std::cerr << "Failed to parse " << kind << " file: " << std::endl |
| 160 | << reader.getFormatedErrorMessages() << std::endl; |
| 161 | return 1; |
| 162 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 163 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 164 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 165 | if (!parseOnly) { |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 166 | FILE* factual = fopen(actual.c_str(), "wt"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 167 | if (!factual) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 168 | std::cerr << "Failed to create '" << kind << "' actual file." |
| 169 | << std::endl; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 170 | return 2; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 171 | } |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 172 | printValueTree(factual, *root); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 173 | fclose(factual); |
| 174 | } |
| 175 | return 0; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 176 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 177 | // static Json::String useFastWriter(Json::Value const& root) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 178 | // Json::FastWriter writer; |
| 179 | // writer.enableYAMLCompatibility(); |
| 180 | // return writer.write(root); |
| 181 | // } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 182 | static Json::String useStyledWriter(Json::Value const& root) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 183 | Json::StyledWriter writer; |
| 184 | return writer.write(root); |
| 185 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 186 | static Json::String useStyledStreamWriter(Json::Value const& root) { |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 187 | Json::StyledStreamWriter writer; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 188 | Json::OStringStream sout; |
Christopher Dunn | 2160c9a | 2015-01-23 09:02:44 -0600 | [diff] [blame] | 189 | writer.write(sout, root); |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 190 | return sout.str(); |
| 191 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 192 | static Json::String useBuiltStyledStreamWriter(Json::Value const& root) { |
Christopher Dunn | 6065a1c | 2015-01-26 11:01:15 -0600 | [diff] [blame] | 193 | Json::StreamWriterBuilder builder; |
Christopher Dunn | 694dbcb | 2015-02-09 15:25:57 -0600 | [diff] [blame] | 194 | return Json::writeString(builder, root); |
Christopher Dunn | 9243d60 | 2015-01-23 08:38:32 -0600 | [diff] [blame] | 195 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 196 | static int rewriteValueTree(const Json::String& rewritePath, |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 197 | const Json::Value& root, |
| 198 | Options::writeFuncType write, |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 199 | Json::String* rewrite) { |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 200 | *rewrite = write(root); |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 201 | FILE* fout = fopen(rewritePath.c_str(), "wt"); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 202 | if (!fout) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 203 | std::cerr << "Failed to create rewrite file: " << rewritePath << std::endl; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 204 | return 2; |
| 205 | } |
Christopher Dunn | 632c9b5 | 2015-01-23 11:09:04 -0600 | [diff] [blame] | 206 | fprintf(fout, "%s\n", rewrite->c_str()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 207 | fclose(fout); |
| 208 | return 0; |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Billy Donahue | 2b593a9 | 2019-01-18 03:46:57 -0500 | [diff] [blame] | 211 | static Json::String removeSuffix(const Json::String& path, |
| 212 | const Json::String& extension) { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 213 | if (extension.length() >= path.length()) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 214 | return Json::String(""); |
| 215 | Json::String suffix = path.substr(path.length() - extension.length()); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 216 | if (suffix != extension) |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 217 | return Json::String(""); |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 218 | return path.substr(0, path.length() - extension.length()); |
| 219 | } |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 220 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 221 | static void printConfig() { |
| 222 | // Print the configuration used to compile JsonCpp |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 223 | #if defined(JSON_NO_INT64) |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 224 | std::cout << "JSON_NO_INT64=1" << std::endl; |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 225 | #else |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 226 | std::cout << "JSON_NO_INT64=0" << std::endl; |
Baptiste Lepilleur | 201fb2c | 2010-04-19 07:37:41 +0000 | [diff] [blame] | 227 | #endif |
| 228 | } |
| 229 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 230 | static int printUsage(const char* argv[]) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 231 | std::cout << "Usage: " << argv[0] << " [--strict] input-json-file" |
| 232 | << std::endl; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 233 | return 3; |
Baptiste Lepilleur | 8868147 | 2009-11-18 21:38:54 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 236 | static int parseCommandLine(int argc, const char* argv[], Options* opts) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 237 | opts->parseOnly = false; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 238 | opts->write = &useStyledWriter; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 239 | if (argc < 2) { |
| 240 | return printUsage(argv); |
| 241 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 242 | int index = 1; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 243 | if (Json::String(argv[index]) == "--json-checker") { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 244 | opts->features = Json::Features::strictMode(); |
| 245 | opts->parseOnly = true; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 246 | ++index; |
| 247 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 248 | if (Json::String(argv[index]) == "--json-config") { |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 249 | printConfig(); |
| 250 | return 3; |
| 251 | } |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 252 | if (Json::String(argv[index]) == "--json-writer") { |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 253 | ++index; |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 254 | Json::String const writerName(argv[index++]); |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 255 | if (writerName == "StyledWriter") { |
| 256 | opts->write = &useStyledWriter; |
| 257 | } else if (writerName == "StyledStreamWriter") { |
| 258 | opts->write = &useStyledStreamWriter; |
Christopher Dunn | 9243d60 | 2015-01-23 08:38:32 -0600 | [diff] [blame] | 259 | } else if (writerName == "BuiltStyledStreamWriter") { |
| 260 | opts->write = &useBuiltStyledStreamWriter; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 261 | } else { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 262 | std::cerr << "Unknown '--json-writer' " << writerName << std::endl; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 263 | return 4; |
| 264 | } |
| 265 | } |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 266 | if (index == argc || index + 1 < argc) { |
| 267 | return printUsage(argv); |
| 268 | } |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 269 | opts->path = argv[index]; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 270 | return 0; |
| 271 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 272 | |
| 273 | static int runTest(Options const& opts, bool use_legacy) { |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 274 | int exitCode = 0; |
| 275 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 276 | Json::String input = readInputTestFile(opts.path.c_str()); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 277 | if (input.empty()) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 278 | std::cerr << "Invalid input file: " << opts.path << std::endl; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 279 | return 3; |
| 280 | } |
| 281 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 282 | Json::String basePath = removeSuffix(opts.path, ".json"); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 283 | if (!opts.parseOnly && basePath.empty()) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 284 | std::cerr << "Bad input path '" << opts.path |
| 285 | << "'. Must end with '.expected'" << std::endl; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 286 | return 3; |
| 287 | } |
| 288 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 289 | Json::String const actualPath = basePath + ".actual"; |
| 290 | Json::String const rewritePath = basePath + ".rewrite"; |
| 291 | Json::String const rewriteActualPath = basePath + ".actual-rewrite"; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 292 | |
| 293 | Json::Value root; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 294 | exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features, |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 295 | opts.parseOnly, &root, use_legacy); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 296 | if (exitCode || opts.parseOnly) { |
| 297 | return exitCode; |
| 298 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 299 | |
Billy Donahue | 1c2ed7a | 2019-01-17 16:35:29 -0500 | [diff] [blame] | 300 | Json::String rewrite; |
Christopher Dunn | 3682f60 | 2015-01-23 11:46:05 -0600 | [diff] [blame] | 301 | exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite); |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 302 | if (exitCode) { |
| 303 | return exitCode; |
| 304 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 305 | |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 306 | Json::Value rewriteRoot; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 307 | exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite", |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 308 | opts.features, opts.parseOnly, &rewriteRoot, |
| 309 | use_legacy); |
| 310 | |
| 311 | return exitCode; |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 312 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 313 | |
Aaron Jacobs | 11086dd | 2014-09-15 10:15:29 +1000 | [diff] [blame] | 314 | int main(int argc, const char* argv[]) { |
Christopher Dunn | 79211e1 | 2015-01-23 11:27:19 -0600 | [diff] [blame] | 315 | Options opts; |
Gaurav | 6c14548 | 2015-09-22 13:53:19 +0530 | [diff] [blame] | 316 | try { |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 317 | int exitCode = parseCommandLine(argc, argv, &opts); |
| 318 | if (exitCode != 0) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 319 | std::cerr << "Failed to parse command-line." << std::endl; |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 320 | return exitCode; |
| 321 | } |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 322 | |
| 323 | const int modern_return_code = runTest(opts, false); |
| 324 | if (modern_return_code) { |
| 325 | return modern_return_code; |
| 326 | } |
| 327 | |
| 328 | const std::string filename = |
| 329 | opts.path.substr(opts.path.find_last_of("\\/") + 1); |
| 330 | const bool should_run_legacy = (filename.rfind("legacy_", 0) == 0); |
| 331 | if (should_run_legacy) { |
| 332 | return runTest(opts, true); |
| 333 | } |
Billy Donahue | b5e1fe8 | 2018-05-20 16:55:27 -0400 | [diff] [blame] | 334 | } catch (const std::exception& e) { |
Jordan Bayles | 645cd04 | 2019-11-08 19:49:16 -0800 | [diff] [blame] | 335 | std::cerr << "Unhandled exception:" << std::endl << e.what() << std::endl; |
Christopher Dunn | 58c31ac | 2015-01-23 11:36:55 -0600 | [diff] [blame] | 336 | return 1; |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 337 | } |
Christopher Dunn | f986423 | 2007-06-14 21:01:26 +0000 | [diff] [blame] | 338 | } |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 339 | |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 340 | #if defined(__GNUC__) |
Christopher Dunn | 90591c7 | 2017-08-28 08:38:29 -0500 | [diff] [blame] | 341 | #pragma GCC diagnostic pop |
Marcel Raad | 240c85a | 2017-11-10 10:58:43 +0100 | [diff] [blame] | 342 | #endif |