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