blob: c8bbd0d7e1afb56512feec8bc17ac2645db37b6f [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{
20 std::string path;
21 Json::Features features;
22 bool parseOnly;
23 typedef std::string (*writeFuncType)(Json::Value const&);
24 writeFuncType write;
25};
26
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100027static std::string normalizeFloatingPointStr(double value) {
28 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;
35 std::string s(buffer);
36 std::string::size_type index = s.find_last_of("eE");
37 if (index != std::string::npos) {
38 std::string::size_type hasSign =
39 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
40 std::string::size_type exponentStartIndex = index + 1 + hasSign;
41 std::string normalized = s.substr(0, exponentStartIndex);
42 std::string::size_type indexDigit =
43 s.find_first_not_of('0', exponentStartIndex);
44 std::string exponent = "0";
45 if (indexDigit !=
46 std::string::npos) // There is an exponent different from 0
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000047 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 exponent = s.substr(indexDigit);
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000049 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100050 return normalized + exponent;
51 }
52 return s;
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000053}
54
Aaron Jacobs11086dd2014-09-15 10:15:29 +100055static std::string readInputTestFile(const char* path) {
56 FILE* file = fopen(path, "rb");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100057 if (!file)
58 return std::string("");
59 fseek(file, 0, SEEK_END);
60 long size = ftell(file);
61 fseek(file, 0, SEEK_SET);
62 std::string text;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100063 char* buffer = new char[size + 1];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100064 buffer[size] = 0;
65 if (fread(buffer, 1, size, file) == (unsigned long)size)
66 text = buffer;
67 fclose(file);
68 delete[] buffer;
69 return text;
Christopher Dunnf9864232007-06-14 21:01:26 +000070}
71
Christopher Dunnf9864232007-06-14 21:01:26 +000072static void
Aaron Jacobs11086dd2014-09-15 10:15:29 +100073printValueTree(FILE* fout, Json::Value& value, const std::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:
82 fprintf(fout,
83 "%s=%s\n",
84 path.c_str(),
85 Json::valueToString(value.asLargestInt()).c_str());
86 break;
87 case Json::uintValue:
88 fprintf(fout,
89 "%s=%s\n",
90 path.c_str(),
91 Json::valueToString(value.asLargestUInt()).c_str());
92 break;
93 case Json::realValue:
94 fprintf(fout,
95 "%s=%s\n",
96 path.c_str(),
97 normalizeFloatingPointStr(value.asDouble()).c_str());
98 break;
99 case Json::stringValue:
100 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
101 break;
102 case Json::booleanValue:
103 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
104 break;
105 case Json::arrayValue: {
106 fprintf(fout, "%s=[]\n", path.c_str());
107 int size = value.size();
108 for (int index = 0; index < size; ++index) {
109 static char buffer[16];
Aaron Jacobsd2618802013-08-08 23:08:28 +0000110#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000111 sprintf_s(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000112#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000113 snprintf(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000114#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000115 printValueTree(fout, value[index], path + buffer);
116 }
117 } break;
118 case Json::objectValue: {
119 fprintf(fout, "%s={}\n", path.c_str());
120 Json::Value::Members members(value.getMemberNames());
121 std::sort(members.begin(), members.end());
122 std::string suffix = *(path.end() - 1) == '.' ? "" : ".";
123 for (Json::Value::Members::iterator it = members.begin();
124 it != members.end();
125 ++it) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000126 const std::string& name = *it;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000127 printValueTree(fout, value[name], path + suffix + name);
128 }
129 } break;
130 default:
131 break;
132 }
Cory Quammen4d234922014-10-09 16:29:47 -0400133
134 if (value.hasComment(Json::commentAfter)) {
135 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
136 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000137}
138
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000139static int parseAndSaveValueTree(const std::string& input,
140 const std::string& actual,
141 const std::string& kind,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000142 const Json::Features& features,
Christopher Dunn632c9b52015-01-23 11:09:04 -0600143 bool parseOnly,
Christopher Dunn79211e12015-01-23 11:27:19 -0600144 Json::Value* root)
145{
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000146 Json::Reader reader(features);
Christopher Dunn632c9b52015-01-23 11:09:04 -0600147 bool parsingSuccessful = reader.parse(input, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000148 if (!parsingSuccessful) {
149 printf("Failed to parse %s file: \n%s\n",
150 kind.c_str(),
151 reader.getFormattedErrorMessages().c_str());
152 return 1;
153 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000154 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000155 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000156 if (!factual) {
157 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000158 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000159 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600160 printValueTree(factual, *root);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000161 fclose(factual);
162 }
163 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000164}
Christopher Dunn79211e12015-01-23 11:27:19 -0600165// static std::string useFastWriter(Json::Value const& root) {
166// Json::FastWriter writer;
167// writer.enableYAMLCompatibility();
168// return writer.write(root);
169// }
170static std::string useStyledWriter(
171 Json::Value const& root)
172{
173 Json::StyledWriter writer;
174 return writer.write(root);
175}
176static std::string useStyledStreamWriter(
177 Json::Value const& root)
178{
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600179 Json::StyledStreamWriter writer;
180 std::ostringstream sout;
181 writer.write(sout, root);
Christopher Dunn79211e12015-01-23 11:27:19 -0600182 return sout.str();
183}
Christopher Dunn9243d602015-01-23 08:38:32 -0600184static std::string useBuiltStyledStreamWriter(
185 Json::Value const& root)
186{
Christopher Dunn6065a1c2015-01-26 11:01:15 -0600187 Json::StreamWriterBuilder builder;
Christopher Dunn694dbcb2015-02-09 15:25:57 -0600188 return Json::writeString(builder, root);
Christopher Dunn9243d602015-01-23 08:38:32 -0600189}
Christopher Dunn79211e12015-01-23 11:27:19 -0600190static int rewriteValueTree(
191 const std::string& rewritePath,
192 const Json::Value& root,
Christopher Dunn3682f602015-01-23 11:46:05 -0600193 Options::writeFuncType write,
Christopher Dunn79211e12015-01-23 11:27:19 -0600194 std::string* rewrite)
195{
Christopher Dunn3682f602015-01-23 11:46:05 -0600196 *rewrite = write(root);
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000197 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000198 if (!fout) {
199 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
200 return 2;
201 }
Christopher Dunn632c9b52015-01-23 11:09:04 -0600202 fprintf(fout, "%s\n", rewrite->c_str());
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000203 fclose(fout);
204 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000205}
206
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000207static std::string removeSuffix(const std::string& path,
208 const std::string& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000209 if (extension.length() >= path.length())
210 return std::string("");
211 std::string suffix = path.substr(path.length() - extension.length());
212 if (suffix != extension)
213 return std::string("");
214 return path.substr(0, path.length() - extension.length());
215}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000216
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217static void printConfig() {
218// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000219#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000220 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000221#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000223#endif
224}
225
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000226static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000227 printf("Usage: %s [--strict] input-json-file", argv[0]);
228 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000229}
230
Christopher Dunn79211e12015-01-23 11:27:19 -0600231static int parseCommandLine(
232 int argc, const char* argv[], Options* opts)
233{
234 opts->parseOnly = false;
Christopher Dunn3682f602015-01-23 11:46:05 -0600235 opts->write = &useStyledWriter;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000236 if (argc < 2) {
237 return printUsage(argv);
238 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000239 int index = 1;
Christopher Dunn3682f602015-01-23 11:46:05 -0600240 if (std::string(argv[index]) == "--json-checker") {
Christopher Dunn79211e12015-01-23 11:27:19 -0600241 opts->features = Json::Features::strictMode();
242 opts->parseOnly = true;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000243 ++index;
244 }
Christopher Dunn08cfd022015-01-23 11:33:47 -0600245 if (std::string(argv[index]) == "--json-config") {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 printConfig();
247 return 3;
248 }
Christopher Dunn3682f602015-01-23 11:46:05 -0600249 if (std::string(argv[index]) == "--json-writer") {
250 ++index;
251 std::string const writerName(argv[index++]);
252 if (writerName == "StyledWriter") {
253 opts->write = &useStyledWriter;
254 } else if (writerName == "StyledStreamWriter") {
255 opts->write = &useStyledStreamWriter;
Christopher Dunn9243d602015-01-23 08:38:32 -0600256 } else if (writerName == "BuiltStyledStreamWriter") {
257 opts->write = &useBuiltStyledStreamWriter;
Christopher Dunn3682f602015-01-23 11:46:05 -0600258 } else {
259 printf("Unknown '--json-writer %s'\n", writerName.c_str());
260 return 4;
261 }
262 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000263 if (index == argc || index + 1 < argc) {
264 return printUsage(argv);
265 }
Christopher Dunn79211e12015-01-23 11:27:19 -0600266 opts->path = argv[index];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000267 return 0;
268}
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600269static int runTest(Options const& opts)
Christopher Dunn79211e12015-01-23 11:27:19 -0600270{
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600271 int exitCode = 0;
272
273 std::string input = readInputTestFile(opts.path.c_str());
274 if (input.empty()) {
275 printf("Failed to read input or empty input: %s\n", opts.path.c_str());
276 return 3;
277 }
278
279 std::string basePath = removeSuffix(opts.path, ".json");
280 if (!opts.parseOnly && basePath.empty()) {
281 printf("Bad input path. Path does not end with '.expected':\n%s\n",
282 opts.path.c_str());
283 return 3;
284 }
285
286 std::string const actualPath = basePath + ".actual";
287 std::string const rewritePath = basePath + ".rewrite";
288 std::string const rewriteActualPath = basePath + ".actual-rewrite";
289
290 Json::Value root;
291 exitCode = parseAndSaveValueTree(
292 input, actualPath, "input",
293 opts.features, opts.parseOnly, &root);
294 if (exitCode || opts.parseOnly) {
295 return exitCode;
296 }
297 std::string rewrite;
Christopher Dunn3682f602015-01-23 11:46:05 -0600298 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600299 if (exitCode) {
300 return exitCode;
301 }
302 Json::Value rewriteRoot;
303 exitCode = parseAndSaveValueTree(
304 rewrite, rewriteActualPath, "rewrite",
305 opts.features, opts.parseOnly, &rewriteRoot);
306 if (exitCode) {
307 return exitCode;
308 }
309 return 0;
Christopher Dunn79211e12015-01-23 11:27:19 -0600310}
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000311int main(int argc, const char* argv[]) {
Christopher Dunn79211e12015-01-23 11:27:19 -0600312 Options opts;
Gaurav6c145482015-09-22 13:53:19 +0530313 try {
Christopher Dunn79211e12015-01-23 11:27:19 -0600314 int exitCode = parseCommandLine(argc, argv, &opts);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000315 if (exitCode != 0) {
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600316 printf("Failed to parse command-line.");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000317 return exitCode;
318 }
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600319 return runTest(opts);
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000320 }
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000321 catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000322 printf("Unhandled exception:\n%s\n", e.what());
Christopher Dunn58c31ac2015-01-23 11:36:55 -0600323 return 1;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000324 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000325}