blob: a0ef94cc41f6d5854a94a18b37f863f0c1f4b27b [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 {
Christopher Dawes75570d72016-03-07 08:29:59 +000022 JSONCPP_STRING path;
Christopher Dunn3682f602015-01-23 11:46:05 -060023 Json::Features features;
24 bool parseOnly;
Christopher Dawes75570d72016-03-07 08:29:59 +000025 typedef JSONCPP_STRING (*writeFuncType)(Json::Value const&);
Christopher Dunn3682f602015-01-23 11:46:05 -060026 writeFuncType write;
27};
28
Christopher Dawes75570d72016-03-07 08:29:59 +000029static JSONCPP_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;
Christopher Dawes75570d72016-03-07 08:29:59 +000033 JSONCPP_STRING s(buffer);
Christopher Dawes75570d72016-03-07 08:29:59 +000034 JSONCPP_STRING::size_type index = s.find_last_of("eE");
35 if (index != JSONCPP_STRING::npos) {
36 JSONCPP_STRING::size_type hasSign =
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100037 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
Christopher Dawes75570d72016-03-07 08:29:59 +000038 JSONCPP_STRING::size_type exponentStartIndex = index + 1 + hasSign;
39 JSONCPP_STRING normalized = s.substr(0, exponentStartIndex);
40 JSONCPP_STRING::size_type indexDigit =
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100041 s.find_first_not_of('0', exponentStartIndex);
Christopher Dawes75570d72016-03-07 08:29:59 +000042 JSONCPP_STRING exponent = "0";
Billy Donahueb5e1fe82018-05-20 16:55:27 -040043 if (indexDigit != JSONCPP_STRING::npos) // There is an exponent different
44 // 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
Christopher Dawes75570d72016-03-07 08:29:59 +000053static JSONCPP_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)
Christopher Dawes75570d72016-03-07 08:29:59 +000056 return JSONCPP_STRING("");
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);
Christopher Dawes75570d72016-03-07 08:29:59 +000061 JSONCPP_STRING text;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100062 char* buffer = new char[size + 1];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100063 buffer[size] = 0;
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 Donahueb5e1fe82018-05-20 16:55:27 -040071static void printValueTree(FILE* fout,
72 Json::Value& value,
73 const JSONCPP_STRING& path = ".") {
Cory Quammen4d234922014-10-09 16:29:47 -040074 if (value.hasComment(Json::commentBefore)) {
75 fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
76 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100077 switch (value.type()) {
78 case Json::nullValue:
79 fprintf(fout, "%s=null\n", path.c_str());
80 break;
81 case Json::intValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040082 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100083 Json::valueToString(value.asLargestInt()).c_str());
84 break;
85 case Json::uintValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040086 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100087 Json::valueToString(value.asLargestUInt()).c_str());
88 break;
89 case Json::realValue:
Billy Donahueb5e1fe82018-05-20 16:55:27 -040090 fprintf(fout, "%s=%s\n", path.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100091 normalizeFloatingPointStr(value.asDouble()).c_str());
92 break;
93 case Json::stringValue:
94 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
95 break;
96 case Json::booleanValue:
97 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
98 break;
99 case Json::arrayValue: {
100 fprintf(fout, "%s=[]\n", path.c_str());
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600101 Json::ArrayIndex size = value.size();
102 for (Json::ArrayIndex index = 0; index < size; ++index) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000103 static char buffer[16];
Hans Johnson5c8e5392018-12-12 13:31:55 -0600104 jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000105 printValueTree(fout, value[index], path + buffer);
106 }
107 } break;
108 case Json::objectValue: {
109 fprintf(fout, "%s={}\n", path.c_str());
110 Json::Value::Members members(value.getMemberNames());
111 std::sort(members.begin(), members.end());
Christopher Dawes75570d72016-03-07 08:29:59 +0000112 JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : ".";
Hans Johnsoncbeed7b2019-01-14 17:09:12 -0600113 for (auto name : members) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000114 printValueTree(fout, value[name], path + suffix + name);
115 }
116 } break;
117 default:
118 break;
119 }
Cory Quammen4d234922014-10-09 16:29:47 -0400120
121 if (value.hasComment(Json::commentAfter)) {
122 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
123 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000124}
125
Christopher Dawes75570d72016-03-07 08:29:59 +0000126static int parseAndSaveValueTree(const JSONCPP_STRING& input,
127 const JSONCPP_STRING& actual,
128 const JSONCPP_STRING& kind,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000129 const Json::Features& features,
Christopher Dunn632c9b52015-01-23 11:09:04 -0600130 bool parseOnly,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400131 Json::Value* root) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000132 Json::Reader reader(features);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400133 bool parsingSuccessful =
134 reader.parse(input.data(), input.data() + input.size(), *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000135 if (!parsingSuccessful) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400136 printf("Failed to parse %s file: \n%s\n", kind.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137 reader.getFormattedErrorMessages().c_str());
138 return 1;
139 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000140 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000141 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142 if (!factual) {
143 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000144 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000145 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600146 printValueTree(factual, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147 fclose(factual);
148 }
149 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000150}
Christopher Dawes75570d72016-03-07 08:29:59 +0000151// static JSONCPP_STRING useFastWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600152// Json::FastWriter writer;
153// writer.enableYAMLCompatibility();
154// return writer.write(root);
155// }
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400156static JSONCPP_STRING useStyledWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600157 Json::StyledWriter writer;
158 return writer.write(root);
159}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400160static JSONCPP_STRING useStyledStreamWriter(Json::Value const& root) {
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600161 Json::StyledStreamWriter writer;
Christopher Dunn38bb4912016-03-06 11:50:00 -0600162 JSONCPP_OSTRINGSTREAM sout;
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600163 writer.write(sout, root);
Christopher Dunn79211e12015-01-23 11:27:19 -0600164 return sout.str();
165}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400166static JSONCPP_STRING useBuiltStyledStreamWriter(Json::Value const& root) {
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600167 Json::StreamWriterBuilder builder;
Christopher Dunn694dbcb2015-02-09 15:25:57 -0600168 return Json::writeString(builder, root);
Christopher Dunn9243d602015-01-23 08:38:32 -0600169}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400170static int rewriteValueTree(const JSONCPP_STRING& rewritePath,
171 const Json::Value& root,
172 Options::writeFuncType write,
173 JSONCPP_STRING* rewrite) {
Christopher Dunn3682f602015-01-23 11:46:05 -0600174 *rewrite = write(root);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000175 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000176 if (!fout) {
177 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
178 return 2;
179 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600180 fprintf(fout, "%s\n", rewrite->c_str());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000181 fclose(fout);
182 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000183}
184
Christopher Dawes75570d72016-03-07 08:29:59 +0000185static JSONCPP_STRING removeSuffix(const JSONCPP_STRING& path,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400186 const JSONCPP_STRING& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000187 if (extension.length() >= path.length())
Christopher Dawes75570d72016-03-07 08:29:59 +0000188 return JSONCPP_STRING("");
189 JSONCPP_STRING suffix = path.substr(path.length() - extension.length());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000190 if (suffix != extension)
Christopher Dawes75570d72016-03-07 08:29:59 +0000191 return JSONCPP_STRING("");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192 return path.substr(0, path.length() - extension.length());
193}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000194
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000195static void printConfig() {
196// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000197#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000199#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000201#endif
202}
203
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000204static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 printf("Usage: %s [--strict] input-json-file", argv[0]);
206 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000207}
208
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400209static int parseCommandLine(int argc, const char* argv[], Options* opts) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600210 opts->parseOnly = false;
Christopher Dunn3682f602015-01-23 11:46:05 -0600211 opts->write = &useStyledWriter;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000212 if (argc < 2) {
213 return printUsage(argv);
214 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000215 int index = 1;
Christopher Dawes75570d72016-03-07 08:29:59 +0000216 if (JSONCPP_STRING(argv[index]) == "--json-checker") {
Christopher Dunn79211e12015-01-23 11:27:19 -0600217 opts->features = Json::Features::strictMode();
218 opts->parseOnly = true;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 ++index;
220 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000221 if (JSONCPP_STRING(argv[index]) == "--json-config") {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 printConfig();
223 return 3;
224 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000225 if (JSONCPP_STRING(argv[index]) == "--json-writer") {
Christopher Dunn3682f602015-01-23 11:46:05 -0600226 ++index;
Christopher Dawes75570d72016-03-07 08:29:59 +0000227 JSONCPP_STRING const writerName(argv[index++]);
Christopher Dunn3682f602015-01-23 11:46:05 -0600228 if (writerName == "StyledWriter") {
229 opts->write = &useStyledWriter;
230 } else if (writerName == "StyledStreamWriter") {
231 opts->write = &useStyledStreamWriter;
Christopher Dunn9243d602015-01-23 08:38:32 -0600232 } else if (writerName == "BuiltStyledStreamWriter") {
233 opts->write = &useBuiltStyledStreamWriter;
Christopher Dunn3682f602015-01-23 11:46:05 -0600234 } else {
235 printf("Unknown '--json-writer %s'\n", writerName.c_str());
236 return 4;
237 }
238 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239 if (index == argc || index + 1 < argc) {
240 return printUsage(argv);
241 }
Christopher Dunn79211e12015-01-23 11:27:19 -0600242 opts->path = argv[index];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 return 0;
244}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400245static int runTest(Options const& opts) {
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600246 int exitCode = 0;
247
Christopher Dawes75570d72016-03-07 08:29:59 +0000248 JSONCPP_STRING input = readInputTestFile(opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600249 if (input.empty()) {
250 printf("Failed to read input or empty input: %s\n", opts.path.c_str());
251 return 3;
252 }
253
Christopher Dawes75570d72016-03-07 08:29:59 +0000254 JSONCPP_STRING basePath = removeSuffix(opts.path, ".json");
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600255 if (!opts.parseOnly && basePath.empty()) {
256 printf("Bad input path. Path does not end with '.expected':\n%s\n",
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400257 opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600258 return 3;
259 }
260
Christopher Dawes75570d72016-03-07 08:29:59 +0000261 JSONCPP_STRING const actualPath = basePath + ".actual";
262 JSONCPP_STRING const rewritePath = basePath + ".rewrite";
263 JSONCPP_STRING const rewriteActualPath = basePath + ".actual-rewrite";
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600264
265 Json::Value root;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400266 exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features,
267 opts.parseOnly, &root);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600268 if (exitCode || opts.parseOnly) {
269 return exitCode;
270 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000271 JSONCPP_STRING rewrite;
Christopher Dunn3682f602015-01-23 11:46:05 -0600272 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600273 if (exitCode) {
274 return exitCode;
275 }
276 Json::Value rewriteRoot;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400277 exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite",
278 opts.features, opts.parseOnly, &rewriteRoot);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600279 if (exitCode) {
280 return exitCode;
281 }
282 return 0;
Christopher Dunn79211e12015-01-23 11:27:19 -0600283}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000284int main(int argc, const char* argv[]) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600285 Options opts;
Gaurav6c145482015-09-22 13:53:19 +0530286 try {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400287 int exitCode = parseCommandLine(argc, argv, &opts);
288 if (exitCode != 0) {
289 printf("Failed to parse command-line.");
290 return exitCode;
291 }
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600292 return runTest(opts);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400293 } catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000294 printf("Unhandled exception:\n%s\n", e.what());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600295 return 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000297}
Christopher Dunn90591c72017-08-28 08:38:29 -0500298
Marcel Raad240c85a2017-11-10 10:58:43 +0100299#if defined(__GNUC__)
Christopher Dunn90591c72017-08-28 08:38:29 -0500300#pragma GCC diagnostic pop
Marcel Raad240c85a2017-11-10 10:58:43 +0100301#endif