blob: 60f7e320cecb8151ba3bffb240f1b8b7fa5ed31a [file] [log] [blame]
Baptiste Lepilleur7469f1d2010-04-20 21:35:19 +00001// Copyright 2007-2010 Baptiste Lepilleur
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
Baptiste Lepilleurfa130ef2010-12-24 12:47:14 +00006/* This executable is used for testing parser/writer using real JSON files.
7 */
8
Christopher Dunnf9864232007-06-14 21:01:26 +00009#include <json/json.h>
10#include <algorithm> // sort
Christopher Dunn2160c9a2015-01-23 09:02:44 -060011#include <sstream>
Christopher Dunnf9864232007-06-14 21:01:26 +000012#include <stdio.h>
13
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100014#if defined(_MSC_VER) && _MSC_VER >= 1310
15#pragma warning(disable : 4996) // disable fopen deprecation warning
Christopher Dunnf9864232007-06-14 21:01:26 +000016#endif
17
Christopher Dunn3682f602015-01-23 11:46:05 -060018struct Options
19{
Christopher Dawes75570d72016-03-07 08:29:59 +000020 JSONCPP_STRING path;
Christopher Dunn3682f602015-01-23 11:46:05 -060021 Json::Features features;
22 bool parseOnly;
Christopher Dawes75570d72016-03-07 08:29:59 +000023 typedef JSONCPP_STRING (*writeFuncType)(Json::Value const&);
Christopher Dunn3682f602015-01-23 11:46:05 -060024 writeFuncType write;
25};
26
Christopher Dawes75570d72016-03-07 08:29:59 +000027static JSONCPP_STRING normalizeFloatingPointStr(double value) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100028 char buffer[32];
Aaron Jacobsd2618802013-08-08 23:08:28 +000029#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030 sprintf_s(buffer, sizeof(buffer), "%.16g", value);
Aaron Jacobsd2618802013-08-08 23:08:28 +000031#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100032 snprintf(buffer, sizeof(buffer), "%.16g", value);
Aaron Jacobsd2618802013-08-08 23:08:28 +000033#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100034 buffer[sizeof(buffer) - 1] = 0;
Christopher Dawes75570d72016-03-07 08:29:59 +000035 JSONCPP_STRING s(buffer);
36#if JSON_USE_SECURE_MEMORY
37 memset(&buffer, 0, sizeof(buffer));
38#endif
39 JSONCPP_STRING::size_type index = s.find_last_of("eE");
40 if (index != JSONCPP_STRING::npos) {
41 JSONCPP_STRING::size_type hasSign =
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100042 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
Christopher Dawes75570d72016-03-07 08:29:59 +000043 JSONCPP_STRING::size_type exponentStartIndex = index + 1 + hasSign;
44 JSONCPP_STRING normalized = s.substr(0, exponentStartIndex);
45 JSONCPP_STRING::size_type indexDigit =
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100046 s.find_first_not_of('0', exponentStartIndex);
Christopher Dawes75570d72016-03-07 08:29:59 +000047 JSONCPP_STRING exponent = "0";
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 if (indexDigit !=
Christopher Dawes75570d72016-03-07 08:29:59 +000049 JSONCPP_STRING::npos) // There is an exponent different from 0
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000050 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100051 exponent = s.substr(indexDigit);
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000052 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100053 return normalized + exponent;
54 }
55 return s;
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000056}
57
Christopher Dawes75570d72016-03-07 08:29:59 +000058static JSONCPP_STRING readInputTestFile(const char* path) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +100059 FILE* file = fopen(path, "rb");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100060 if (!file)
Christopher Dawes75570d72016-03-07 08:29:59 +000061 return JSONCPP_STRING("");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100062 fseek(file, 0, SEEK_END);
Christopher Dunnd4513fc2016-02-06 09:25:20 -060063 long const size = ftell(file);
64 unsigned long const usize = static_cast<unsigned long const>(size);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100065 fseek(file, 0, SEEK_SET);
Christopher Dawes75570d72016-03-07 08:29:59 +000066 JSONCPP_STRING text;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100067 char* buffer = new char[size + 1];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100068 buffer[size] = 0;
Christopher Dunnd4513fc2016-02-06 09:25:20 -060069 if (fread(buffer, 1, usize, file) == usize)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100070 text = buffer;
71 fclose(file);
Christopher Dawes75570d72016-03-07 08:29:59 +000072#if JSON_USE_SECURE_MEMORY
73 memset(buffer, 0, static_cast<size_t>(size + 1));
74#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100075 delete[] buffer;
76 return text;
Christopher Dunnf9864232007-06-14 21:01:26 +000077}
78
Christopher Dunnf9864232007-06-14 21:01:26 +000079static void
Christopher Dawes75570d72016-03-07 08:29:59 +000080printValueTree(FILE* fout, Json::Value& value, const JSONCPP_STRING& path = ".") {
Cory Quammen4d234922014-10-09 16:29:47 -040081 if (value.hasComment(Json::commentBefore)) {
82 fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
83 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100084 switch (value.type()) {
85 case Json::nullValue:
86 fprintf(fout, "%s=null\n", path.c_str());
87 break;
88 case Json::intValue:
89 fprintf(fout,
90 "%s=%s\n",
91 path.c_str(),
92 Json::valueToString(value.asLargestInt()).c_str());
93 break;
94 case Json::uintValue:
95 fprintf(fout,
96 "%s=%s\n",
97 path.c_str(),
98 Json::valueToString(value.asLargestUInt()).c_str());
99 break;
100 case Json::realValue:
101 fprintf(fout,
102 "%s=%s\n",
103 path.c_str(),
104 normalizeFloatingPointStr(value.asDouble()).c_str());
105 break;
106 case Json::stringValue:
107 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
108 break;
109 case Json::booleanValue:
110 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
111 break;
112 case Json::arrayValue: {
113 fprintf(fout, "%s=[]\n", path.c_str());
Christopher Dunnd4513fc2016-02-06 09:25:20 -0600114 Json::ArrayIndex size = value.size();
115 for (Json::ArrayIndex index = 0; index < size; ++index) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000116 static char buffer[16];
Aaron Jacobsd2618802013-08-08 23:08:28 +0000117#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118 sprintf_s(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000119#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000120 snprintf(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000121#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000122 printValueTree(fout, value[index], path + buffer);
123 }
124 } break;
125 case Json::objectValue: {
126 fprintf(fout, "%s={}\n", path.c_str());
127 Json::Value::Members members(value.getMemberNames());
128 std::sort(members.begin(), members.end());
Christopher Dawes75570d72016-03-07 08:29:59 +0000129 JSONCPP_STRING suffix = *(path.end() - 1) == '.' ? "" : ".";
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000130 for (Json::Value::Members::iterator it = members.begin();
131 it != members.end();
132 ++it) {
Christopher Dawes75570d72016-03-07 08:29:59 +0000133 const JSONCPP_STRING name = *it;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000134 printValueTree(fout, value[name], path + suffix + name);
135 }
136 } break;
137 default:
138 break;
139 }
Cory Quammen4d234922014-10-09 16:29:47 -0400140
141 if (value.hasComment(Json::commentAfter)) {
142 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
143 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000144}
145
Christopher Dawes75570d72016-03-07 08:29:59 +0000146static int parseAndSaveValueTree(const JSONCPP_STRING& input,
147 const JSONCPP_STRING& actual,
148 const JSONCPP_STRING& kind,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000149 const Json::Features& features,
Christopher Dunn632c9b52015-01-23 11:09:04 -0600150 bool parseOnly,
Christopher Dunn79211e12015-01-23 11:27:19 -0600151 Json::Value* root)
152{
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000153 Json::Reader reader(features);
Christopher Dunn632c9b52015-01-23 11:09:04 -0600154 bool parsingSuccessful = reader.parse(input, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000155 if (!parsingSuccessful) {
156 printf("Failed to parse %s file: \n%s\n",
157 kind.c_str(),
158 reader.getFormattedErrorMessages().c_str());
159 return 1;
160 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000162 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000163 if (!factual) {
164 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000165 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000166 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600167 printValueTree(factual, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000168 fclose(factual);
169 }
170 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000171}
Christopher Dawes75570d72016-03-07 08:29:59 +0000172// static JSONCPP_STRING useFastWriter(Json::Value const& root) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600173// Json::FastWriter writer;
174// writer.enableYAMLCompatibility();
175// return writer.write(root);
176// }
Christopher Dawes75570d72016-03-07 08:29:59 +0000177static JSONCPP_STRING useStyledWriter(
Christopher Dunn79211e12015-01-23 11:27:19 -0600178 Json::Value const& root)
179{
180 Json::StyledWriter writer;
181 return writer.write(root);
182}
Christopher Dawes75570d72016-03-07 08:29:59 +0000183static JSONCPP_STRING useStyledStreamWriter(
Christopher Dunn79211e12015-01-23 11:27:19 -0600184 Json::Value const& root)
185{
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600186 Json::StyledStreamWriter writer;
Christopher Dunn38bb4912016-03-06 11:50:00 -0600187 JSONCPP_OSTRINGSTREAM sout;
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600188 writer.write(sout, root);
Christopher Dunn79211e12015-01-23 11:27:19 -0600189 return sout.str();
190}
Christopher Dawes75570d72016-03-07 08:29:59 +0000191static JSONCPP_STRING useBuiltStyledStreamWriter(
Christopher Dunn9243d602015-01-23 08:38:32 -0600192 Json::Value const& root)
193{
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600194 Json::StreamWriterBuilder builder;
Christopher Dunn694dbcb2015-02-09 15:25:57 -0600195 return Json::writeString(builder, root);
Christopher Dunn9243d602015-01-23 08:38:32 -0600196}
Christopher Dunn79211e12015-01-23 11:27:19 -0600197static int rewriteValueTree(
Christopher Dawes75570d72016-03-07 08:29:59 +0000198 const JSONCPP_STRING& rewritePath,
Christopher Dunn79211e12015-01-23 11:27:19 -0600199 const Json::Value& root,
Christopher Dunn3682f602015-01-23 11:46:05 -0600200 Options::writeFuncType write,
Christopher Dawes75570d72016-03-07 08:29:59 +0000201 JSONCPP_STRING* rewrite)
Christopher Dunn79211e12015-01-23 11:27:19 -0600202{
Christopher Dunn3682f602015-01-23 11:46:05 -0600203 *rewrite = write(root);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000204 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 if (!fout) {
206 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
207 return 2;
208 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600209 fprintf(fout, "%s\n", rewrite->c_str());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000210 fclose(fout);
211 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000212}
213
Christopher Dawes75570d72016-03-07 08:29:59 +0000214static JSONCPP_STRING removeSuffix(const JSONCPP_STRING& path,
215 const JSONCPP_STRING& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000216 if (extension.length() >= path.length())
Christopher Dawes75570d72016-03-07 08:29:59 +0000217 return JSONCPP_STRING("");
218 JSONCPP_STRING suffix = path.substr(path.length() - extension.length());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000219 if (suffix != extension)
Christopher Dawes75570d72016-03-07 08:29:59 +0000220 return JSONCPP_STRING("");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000221 return path.substr(0, path.length() - extension.length());
222}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000223
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000224static void printConfig() {
225// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000226#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000228#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000229 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000230#endif
231}
232
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000233static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000234 printf("Usage: %s [--strict] input-json-file", argv[0]);
235 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000236}
237
Christopher Dunn79211e12015-01-23 11:27:19 -0600238static int parseCommandLine(
239 int argc, const char* argv[], Options* opts)
240{
241 opts->parseOnly = false;
Christopher Dunn3682f602015-01-23 11:46:05 -0600242 opts->write = &useStyledWriter;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 if (argc < 2) {
244 return printUsage(argv);
245 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 int index = 1;
Christopher Dawes75570d72016-03-07 08:29:59 +0000247 if (JSONCPP_STRING(argv[index]) == "--json-checker") {
Christopher Dunn79211e12015-01-23 11:27:19 -0600248 opts->features = Json::Features::strictMode();
249 opts->parseOnly = true;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000250 ++index;
251 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000252 if (JSONCPP_STRING(argv[index]) == "--json-config") {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 printConfig();
254 return 3;
255 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000256 if (JSONCPP_STRING(argv[index]) == "--json-writer") {
Christopher Dunn3682f602015-01-23 11:46:05 -0600257 ++index;
Christopher Dawes75570d72016-03-07 08:29:59 +0000258 JSONCPP_STRING const writerName(argv[index++]);
Christopher Dunn3682f602015-01-23 11:46:05 -0600259 if (writerName == "StyledWriter") {
260 opts->write = &useStyledWriter;
261 } else if (writerName == "StyledStreamWriter") {
262 opts->write = &useStyledStreamWriter;
Christopher Dunn9243d602015-01-23 08:38:32 -0600263 } else if (writerName == "BuiltStyledStreamWriter") {
264 opts->write = &useBuiltStyledStreamWriter;
Christopher Dunn3682f602015-01-23 11:46:05 -0600265 } else {
266 printf("Unknown '--json-writer %s'\n", writerName.c_str());
267 return 4;
268 }
269 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000270 if (index == argc || index + 1 < argc) {
271 return printUsage(argv);
272 }
Christopher Dunn79211e12015-01-23 11:27:19 -0600273 opts->path = argv[index];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 return 0;
275}
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600276static int runTest(Options const& opts)
Christopher Dunn79211e12015-01-23 11:27:19 -0600277{
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600278 int exitCode = 0;
279
Christopher Dawes75570d72016-03-07 08:29:59 +0000280 JSONCPP_STRING input = readInputTestFile(opts.path.c_str());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600281 if (input.empty()) {
282 printf("Failed to read input or empty input: %s\n", opts.path.c_str());
283 return 3;
284 }
285
Christopher Dawes75570d72016-03-07 08:29:59 +0000286 JSONCPP_STRING basePath = removeSuffix(opts.path, ".json");
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600287 if (!opts.parseOnly && basePath.empty()) {
288 printf("Bad input path. Path does not end with '.expected':\n%s\n",
289 opts.path.c_str());
290 return 3;
291 }
292
Christopher Dawes75570d72016-03-07 08:29:59 +0000293 JSONCPP_STRING const actualPath = basePath + ".actual";
294 JSONCPP_STRING const rewritePath = basePath + ".rewrite";
295 JSONCPP_STRING const rewriteActualPath = basePath + ".actual-rewrite";
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600296
297 Json::Value root;
298 exitCode = parseAndSaveValueTree(
299 input, actualPath, "input",
300 opts.features, opts.parseOnly, &root);
301 if (exitCode || opts.parseOnly) {
302 return exitCode;
303 }
Christopher Dawes75570d72016-03-07 08:29:59 +0000304 JSONCPP_STRING rewrite;
Christopher Dunn3682f602015-01-23 11:46:05 -0600305 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600306 if (exitCode) {
307 return exitCode;
308 }
309 Json::Value rewriteRoot;
310 exitCode = parseAndSaveValueTree(
311 rewrite, rewriteActualPath, "rewrite",
312 opts.features, opts.parseOnly, &rewriteRoot);
313 if (exitCode) {
314 return exitCode;
315 }
316 return 0;
Christopher Dunn79211e12015-01-23 11:27:19 -0600317}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000318int main(int argc, const char* argv[]) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600319 Options opts;
Gaurav6c145482015-09-22 13:53:19 +0530320 try {
Christopher Dunn79211e12015-01-23 11:27:19 -0600321 int exitCode = parseCommandLine(argc, argv, &opts);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 if (exitCode != 0) {
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600323 printf("Failed to parse command-line.");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000324 return exitCode;
325 }
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600326 return runTest(opts);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000327 }
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000328 catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000329 printf("Unhandled exception:\n%s\n", e.what());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600330 return 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000331 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000332}