blob: d2d41aa5fd99b1000dd52d53f4882bf76b779b5f [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00002// 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 Raad240c85a2017-11-10 10:58:43 +01006#if defined(__GNUC__)
Christopher Dunn90591c72017-08-28 08:38:29 -05007#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
Marcel Raad240c85a2017-11-10 10:58:43 +01009#elif defined(_MSC_VER)
10#pragma warning(disable : 4996)
11#endif
Christopher Dunn90591c72017-08-28 08:38:29 -050012
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +000013/* This executable is used for testing parser/writer using real JSON files.
14 */
15
Christopher Dunnf9864232007-06-14 21:01:26 +000016#include <algorithm> // sort
Billy Donahuedc4a7f92019-01-17 11:07:53 -050017#include <cstdio>
Billy Donahueb5e1fe82018-05-20 16:55:27 -040018#include <json/json.h>
Christopher Dunn2160c9a2015-01-23 09:02:44 -060019#include <sstream>
Christopher Dunnf9864232007-06-14 21:01:26 +000020
Billy Donahueb5e1fe82018-05-20 16:55:27 -040021struct Options {
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050022 Json::String path;
Christopher Dunn3682f602015-01-23 11:46:05 -060023 Json::Features features;
24 bool parseOnly;
Billy Donahue2b593a92019-01-18 03:46:57 -050025 using writeFuncType = Json::String (*)(Json::Value const&);
Christopher Dunn3682f602015-01-23 11:46:05 -060026 writeFuncType write;
27};
28
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050029static Json::String normalizeFloatingPointStr(double value) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030 char buffer[32];
Hans Johnson5c8e5392018-12-12 13:31:55 -060031 jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100032 buffer[sizeof(buffer) - 1] = 0;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050033 Json::String s(buffer);
34 Json::String::size_type index = s.find_last_of("eE");
35 if (index != Json::String::npos) {
36 Json::String::size_type hasSign =
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100037 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050038 Json::String::size_type exponentStartIndex = index + 1 + hasSign;
39 Json::String normalized = s.substr(0, exponentStartIndex);
Billy Donahue2b593a92019-01-18 03:46:57 -050040 Json::String::size_type indexDigit =
41 s.find_first_not_of('0', exponentStartIndex);
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050042 Json::String exponent = "0";
43 if (indexDigit != Json::String::npos) // There is an exponent different
Billy Donahue2b593a92019-01-18 03:46:57 -050044 // from 0
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000045 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100046 exponent = s.substr(indexDigit);
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000047 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 return normalized + exponent;
49 }
50 return s;
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000051}
52
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050053static Json::String readInputTestFile(const char* path) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +100054 FILE* file = fopen(path, "rb");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100055 if (!file)
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050056 return "";
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100057 fseek(file, 0, SEEK_END);
Christopher Dunnd4513fc2016-02-06 09:25:20 -060058 long const size = ftell(file);
Hans Johnson1fc3de72019-01-14 17:09:15 -060059 size_t const usize = static_cast<unsigned long>(size);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100060 fseek(file, 0, SEEK_SET);
Aaron Jacobs11086dd2014-09-15 10:15:29 +100061 char* buffer = new char[size + 1];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100062 buffer[size] = 0;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050063 Json::String text;
Christopher Dunnd4513fc2016-02-06 09:25:20 -060064 if (fread(buffer, 1, usize, file) == usize)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100065 text = buffer;
66 fclose(file);
67 delete[] buffer;
68 return text;
Christopher Dunnf9864232007-06-14 21:01:26 +000069}
70
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050071static void
72printValueTree(FILE* fout, Json::Value& value, const Json::String& path = ".") {
Cory Quammen4d234922014-10-09 16:29:47 -040073 if (value.hasComment(Json::commentBefore)) {
74 fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
75 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100076 switch (value.type()) {
77 case Json::nullValue:
78 fprintf(fout, "%s=null\n", path.c_str());
79 break;
80 case Json::intValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040081 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100082 Json::valueToString(value.asLargestInt()).c_str());
83 break;
84 case Json::uintValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040085 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100086 Json::valueToString(value.asLargestUInt()).c_str());
87 break;
88 case Json::realValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040089 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100090 normalizeFloatingPointStr(value.asDouble()).c_str());
91 break;
92 case Json::stringValue:
93 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
94 break;
95 case Json::booleanValue:
96 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
97 break;
98 case Json::arrayValue: {
99 fprintf(fout, "%s=[]\n", path.c_str());
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600100 Json::ArrayIndex size = value.size();
101 for (Json::ArrayIndex index = 0; index < size; ++index) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000102 static char buffer[16];
Hans Johnson5c8e5392018-12-12 13:31:55 -0600103 jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000104 printValueTree(fout, value[index], path + buffer);
105 }
106 } break;
107 case Json::objectValue: {
108 fprintf(fout, "%s={}\n", path.c_str());
109 Json::Value::Members members(value.getMemberNames());
110 std::sort(members.begin(), members.end());
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500111 Json::String suffix = *(path.end() - 1) == '.' ? "" : ".";
Hans Johnsoncbeed7b2019-01-14 17:09:12 -0600112 for (auto name : members) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113 printValueTree(fout, value[name], path + suffix + name);
114 }
115 } break;
116 default:
117 break;
118 }
Cory Quammen4d234922014-10-09 16:29:47 -0400119
120 if (value.hasComment(Json::commentAfter)) {
121 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
122 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000123}
124
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500125static int parseAndSaveValueTree(const Json::String& input,
126 const Json::String& actual,
127 const Json::String& kind,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000128 const Json::Features& features,
Christopher Dunn632c9b52015-01-23 11:09:04 -0600129 bool parseOnly,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400130 Json::Value* root) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000131 Json::Reader reader(features);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400132 bool parsingSuccessful =
133 reader.parse(input.data(), input.data() + input.size(), *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134 if (!parsingSuccessful) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400135 printf("Failed to parse %s file: \n%s\n", kind.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000136 reader.getFormattedErrorMessages().c_str());
137 return 1;
138 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000140 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000141 if (!factual) {
142 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000143 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000144 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600145 printValueTree(factual, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146 fclose(factual);
147 }
148 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000149}
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500150// static Json::String useFastWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600151// Json::FastWriter writer;
152// writer.enableYAMLCompatibility();
153// return writer.write(root);
154// }
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500155static Json::String useStyledWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600156 Json::StyledWriter writer;
157 return writer.write(root);
158}
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500159static Json::String useStyledStreamWriter(Json::Value const& root) {
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600160 Json::StyledStreamWriter writer;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500161 Json::OStringStream sout;
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600162 writer.write(sout, root);
Christopher Dunn79211e12015-01-23 11:27:19 -0600163 return sout.str();
164}
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500165static Json::String useBuiltStyledStreamWriter(Json::Value const& root) {
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600166 Json::StreamWriterBuilder builder;
Christopher Dunn694dbcb2015-02-09 15:25:57 -0600167 return Json::writeString(builder, root);
Christopher Dunn9243d602015-01-23 08:38:32 -0600168}
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500169static int rewriteValueTree(const Json::String& rewritePath,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400170 const Json::Value& root,
171 Options::writeFuncType write,
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500172 Json::String* rewrite) {
Christopher Dunn3682f602015-01-23 11:46:05 -0600173 *rewrite = write(root);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000174 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000175 if (!fout) {
176 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
177 return 2;
178 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600179 fprintf(fout, "%s\n", rewrite->c_str());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000180 fclose(fout);
181 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000182}
183
Billy Donahue2b593a92019-01-18 03:46:57 -0500184static Json::String removeSuffix(const Json::String& path,
185 const Json::String& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186 if (extension.length() >= path.length())
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500187 return Json::String("");
188 Json::String suffix = path.substr(path.length() - extension.length());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 if (suffix != extension)
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500190 return Json::String("");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191 return path.substr(0, path.length() - extension.length());
192}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000193
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194static void printConfig() {
195// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000196#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000198#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000199 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000200#endif
201}
202
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000203static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000204 printf("Usage: %s [--strict] input-json-file", argv[0]);
205 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000206}
207
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400208static int parseCommandLine(int argc, const char* argv[], Options* opts) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600209 opts->parseOnly = false;
Christopher Dunn3682f602015-01-23 11:46:05 -0600210 opts->write = &useStyledWriter;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000211 if (argc < 2) {
212 return printUsage(argv);
213 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 int index = 1;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500215 if (Json::String(argv[index]) == "--json-checker") {
Christopher Dunn79211e12015-01-23 11:27:19 -0600216 opts->features = Json::Features::strictMode();
217 opts->parseOnly = true;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000218 ++index;
219 }
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500220 if (Json::String(argv[index]) == "--json-config") {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221 printConfig();
222 return 3;
223 }
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500224 if (Json::String(argv[index]) == "--json-writer") {
Christopher Dunn3682f602015-01-23 11:46:05 -0600225 ++index;
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500226 Json::String const writerName(argv[index++]);
Christopher Dunn3682f602015-01-23 11:46:05 -0600227 if (writerName == "StyledWriter") {
228 opts->write = &useStyledWriter;
229 } else if (writerName == "StyledStreamWriter") {
230 opts->write = &useStyledStreamWriter;
Christopher Dunn9243d602015-01-23 08:38:32 -0600231 } else if (writerName == "BuiltStyledStreamWriter") {
232 opts->write = &useBuiltStyledStreamWriter;
Christopher Dunn3682f602015-01-23 11:46:05 -0600233 } else {
234 printf("Unknown '--json-writer %s'\n", writerName.c_str());
235 return 4;
236 }
237 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000238 if (index == argc || index + 1 < argc) {
239 return printUsage(argv);
240 }
Christopher Dunn79211e12015-01-23 11:27:19 -0600241 opts->path = argv[index];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000242 return 0;
243}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400244static int runTest(Options const& opts) {
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600245 int exitCode = 0;
246
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500247 Json::String input = readInputTestFile(opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600248 if (input.empty()) {
249 printf("Failed to read input or empty input: %s\n", opts.path.c_str());
250 return 3;
251 }
252
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500253 Json::String basePath = removeSuffix(opts.path, ".json");
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600254 if (!opts.parseOnly && basePath.empty()) {
255 printf("Bad input path. Path does not end with '.expected':\n%s\n",
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400256 opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600257 return 3;
258 }
259
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500260 Json::String const actualPath = basePath + ".actual";
261 Json::String const rewritePath = basePath + ".rewrite";
262 Json::String const rewriteActualPath = basePath + ".actual-rewrite";
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600263
264 Json::Value root;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400265 exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features,
266 opts.parseOnly, &root);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600267 if (exitCode || opts.parseOnly) {
268 return exitCode;
269 }
Billy Donahue1c2ed7a2019-01-17 16:35:29 -0500270 Json::String rewrite;
Christopher Dunn3682f602015-01-23 11:46:05 -0600271 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600272 if (exitCode) {
273 return exitCode;
274 }
275 Json::Value rewriteRoot;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400276 exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite",
277 opts.features, opts.parseOnly, &rewriteRoot);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600278 if (exitCode) {
279 return exitCode;
280 }
281 return 0;
Christopher Dunn79211e12015-01-23 11:27:19 -0600282}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000283int main(int argc, const char* argv[]) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600284 Options opts;
Gaurav6c145482015-09-22 13:53:19 +0530285 try {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400286 int exitCode = parseCommandLine(argc, argv, &opts);
287 if (exitCode != 0) {
288 printf("Failed to parse command-line.");
289 return exitCode;
290 }
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600291 return runTest(opts);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400292 } catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000293 printf("Unhandled exception:\n%s\n", e.what());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600294 return 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000295 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000296}
Christopher Dunn90591c72017-08-28 08:38:29 -0500297
Marcel Raad240c85a2017-11-10 10:58:43 +0100298#if defined(__GNUC__)
Christopher Dunn90591c72017-08-28 08:38:29 -0500299#pragma GCC diagnostic pop
Marcel Raad240c85a2017-11-10 10:58:43 +0100300#endif