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