blob: 3b5e21a51f2db96b139ca2d7367bbbdc4eb3c0fa [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 Donahueb5e1fe82018-05-20 16:55:27 -040017#include <json/json.h>
Christopher Dunn2160c9a2015-01-23 09:02:44 -060018#include <sstream>
Christopher Dunnf9864232007-06-14 21:01:26 +000019#include <stdio.h>
20
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);
Christopher Dunnb9996162016-05-15 23:13:47 -050059 unsigned long 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) == '.' ? "" : ".";
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113 for (Json::Value::Members::iterator it = members.begin();
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400114 it != members.end(); ++it) {
Christopher Dawes75570d72016-03-07 08:29:59 +0000115 const JSONCPP_STRING name = *it;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000116 printValueTree(fout, value[name], path + suffix + name);
117 }
118 } break;
119 default:
120 break;
121 }
Cory Quammen4d234922014-10-09 16:29:47 -0400122
123 if (value.hasComment(Json::commentAfter)) {
124 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
125 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000126}
127
Christopher Dawes75570d72016-03-07 08:29:59 +0000128static int parseAndSaveValueTree(const JSONCPP_STRING& input,
129 const JSONCPP_STRING& actual,
130 const JSONCPP_STRING& kind,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000131 const Json::Features& features,
Christopher Dunn632c9b52015-01-23 11:09:04 -0600132 bool parseOnly,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400133 Json::Value* root) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134 Json::Reader reader(features);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400135 bool parsingSuccessful =
136 reader.parse(input.data(), input.data() + input.size(), *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000137 if (!parsingSuccessful) {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400138 printf("Failed to parse %s file: \n%s\n", kind.c_str(),
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000139 reader.getFormattedErrorMessages().c_str());
140 return 1;
141 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000142 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000143 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000144 if (!factual) {
145 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000146 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600148 printValueTree(factual, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000149 fclose(factual);
150 }
151 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000152}
Christopher Dawes75570d72016-03-07 08:29:59 +0000153// static JSONCPP_STRING useFastWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600154// Json::FastWriter writer;
155// writer.enableYAMLCompatibility();
156// return writer.write(root);
157// }
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400158static JSONCPP_STRING useStyledWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600159 Json::StyledWriter writer;
160 return writer.write(root);
161}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400162static JSONCPP_STRING useStyledStreamWriter(Json::Value const& root) {
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600163 Json::StyledStreamWriter writer;
Christopher Dunn38bb4912016-03-06 11:50:00 -0600164 JSONCPP_OSTRINGSTREAM sout;
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600165 writer.write(sout, root);
Christopher Dunn79211e12015-01-23 11:27:19 -0600166 return sout.str();
167}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400168static JSONCPP_STRING useBuiltStyledStreamWriter(Json::Value const& root) {
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600169 Json::StreamWriterBuilder builder;
Christopher Dunn694dbcb2015-02-09 15:25:57 -0600170 return Json::writeString(builder, root);
Christopher Dunn9243d602015-01-23 08:38:32 -0600171}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400172static int rewriteValueTree(const JSONCPP_STRING& rewritePath,
173 const Json::Value& root,
174 Options::writeFuncType write,
175 JSONCPP_STRING* rewrite) {
Christopher Dunn3682f602015-01-23 11:46:05 -0600176 *rewrite = write(root);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000177 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178 if (!fout) {
179 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
180 return 2;
181 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600182 fprintf(fout, "%s\n", rewrite->c_str());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000183 fclose(fout);
184 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000185}
186
Christopher Dawes75570d72016-03-07 08:29:59 +0000187static JSONCPP_STRING removeSuffix(const JSONCPP_STRING& path,
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400188 const JSONCPP_STRING& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 if (extension.length() >= path.length())
Christopher Dawes75570d72016-03-07 08:29:59 +0000190 return JSONCPP_STRING("");
191 JSONCPP_STRING suffix = path.substr(path.length() - extension.length());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000192 if (suffix != extension)
Christopher Dawes75570d72016-03-07 08:29:59 +0000193 return JSONCPP_STRING("");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000194 return path.substr(0, path.length() - extension.length());
195}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000196
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000197static void printConfig() {
198// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000199#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000201#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000202 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000203#endif
204}
205
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000206static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000207 printf("Usage: %s [--strict] input-json-file", argv[0]);
208 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000209}
210
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400211static int parseCommandLine(int argc, const char* argv[], Options* opts) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600212 opts->parseOnly = false;
Christopher Dunn3682f602015-01-23 11:46:05 -0600213 opts->write = &useStyledWriter;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000214 if (argc < 2) {
215 return printUsage(argv);
216 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217 int index = 1;
Christopher Dawes75570d72016-03-07 08:29:59 +0000218 if (JSONCPP_STRING(argv[index]) == "--json-checker") {
Christopher Dunn79211e12015-01-23 11:27:19 -0600219 opts->features = Json::Features::strictMode();
220 opts->parseOnly = true;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221 ++index;
222 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000223 if (JSONCPP_STRING(argv[index]) == "--json-config") {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224 printConfig();
225 return 3;
226 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000227 if (JSONCPP_STRING(argv[index]) == "--json-writer") {
Christopher Dunn3682f602015-01-23 11:46:05 -0600228 ++index;
Christopher Dawes75570d72016-03-07 08:29:59 +0000229 JSONCPP_STRING const writerName(argv[index++]);
Christopher Dunn3682f602015-01-23 11:46:05 -0600230 if (writerName == "StyledWriter") {
231 opts->write = &useStyledWriter;
232 } else if (writerName == "StyledStreamWriter") {
233 opts->write = &useStyledStreamWriter;
Christopher Dunn9243d602015-01-23 08:38:32 -0600234 } else if (writerName == "BuiltStyledStreamWriter") {
235 opts->write = &useBuiltStyledStreamWriter;
Christopher Dunn3682f602015-01-23 11:46:05 -0600236 } else {
237 printf("Unknown '--json-writer %s'\n", writerName.c_str());
238 return 4;
239 }
240 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000241 if (index == argc || index + 1 < argc) {
242 return printUsage(argv);
243 }
Christopher Dunn79211e12015-01-23 11:27:19 -0600244 opts->path = argv[index];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000245 return 0;
246}
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400247static int runTest(Options const& opts) {
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600248 int exitCode = 0;
249
Christopher Dawes75570d72016-03-07 08:29:59 +0000250 JSONCPP_STRING input = readInputTestFile(opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600251 if (input.empty()) {
252 printf("Failed to read input or empty input: %s\n", opts.path.c_str());
253 return 3;
254 }
255
Christopher Dawes75570d72016-03-07 08:29:59 +0000256 JSONCPP_STRING basePath = removeSuffix(opts.path, ".json");
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600257 if (!opts.parseOnly && basePath.empty()) {
258 printf("Bad input path. Path does not end with '.expected':\n%s\n",
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400259 opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600260 return 3;
261 }
262
Christopher Dawes75570d72016-03-07 08:29:59 +0000263 JSONCPP_STRING const actualPath = basePath + ".actual";
264 JSONCPP_STRING const rewritePath = basePath + ".rewrite";
265 JSONCPP_STRING const rewriteActualPath = basePath + ".actual-rewrite";
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600266
267 Json::Value root;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400268 exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features,
269 opts.parseOnly, &root);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600270 if (exitCode || opts.parseOnly) {
271 return exitCode;
272 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000273 JSONCPP_STRING rewrite;
Christopher Dunn3682f602015-01-23 11:46:05 -0600274 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600275 if (exitCode) {
276 return exitCode;
277 }
278 Json::Value rewriteRoot;
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400279 exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite",
280 opts.features, opts.parseOnly, &rewriteRoot);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600281 if (exitCode) {
282 return exitCode;
283 }
284 return 0;
Christopher Dunn79211e12015-01-23 11:27:19 -0600285}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000286int main(int argc, const char* argv[]) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600287 Options opts;
Gaurav6c145482015-09-22 13:53:19 +0530288 try {
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400289 int exitCode = parseCommandLine(argc, argv, &opts);
290 if (exitCode != 0) {
291 printf("Failed to parse command-line.");
292 return exitCode;
293 }
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600294 return runTest(opts);
Billy Donahueb5e1fe82018-05-20 16:55:27 -0400295 } catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000296 printf("Unhandled exception:\n%s\n", e.what());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600297 return 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000298 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000299}
Christopher Dunn90591c72017-08-28 08:38:29 -0500300
Marcel Raad240c85a2017-11-10 10:58:43 +0100301#if defined(__GNUC__)
Christopher Dunn90591c72017-08-28 08:38:29 -0500302#pragma GCC diagnostic pop
Marcel Raad240c85a2017-11-10 10:58:43 +0100303#endif