blob: 3452c5986ac3c3065bf56f8e04d4982c5cfb6c14 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04002// 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
Haibo Huangb0bee822021-02-24 15:40:15 -08006#if defined(__GNUC__)
7#pragma GCC diagnostic push
8#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
9#elif defined(_MSC_VER)
10#pragma warning(disable : 4996)
11#endif
12
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040013/* This executable is used for testing parser/writer using real JSON files.
14 */
15
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040016#include <algorithm> // sort
Haibo Huangb0bee822021-02-24 15:40:15 -080017#include <cstdio>
18#include <iostream>
19#include <json/json.h>
20#include <memory>
21#include <sstream>
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040022
Haibo Huangb0bee822021-02-24 15:40:15 -080023struct Options {
24 Json::String path;
25 Json::Features features;
26 bool parseOnly;
27 using writeFuncType = Json::String (*)(Json::Value const&);
28 writeFuncType write;
29};
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040030
Haibo Huangb0bee822021-02-24 15:40:15 -080031static Json::String normalizeFloatingPointStr(double value) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050032 char buffer[32];
Haibo Huangb0bee822021-02-24 15:40:15 -080033 jsoncpp_snprintf(buffer, sizeof(buffer), "%.16g", value);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050034 buffer[sizeof(buffer) - 1] = 0;
Haibo Huangb0bee822021-02-24 15:40:15 -080035 Json::String s(buffer);
36 Json::String::size_type index = s.find_last_of("eE");
37 if (index != Json::String::npos) {
38 Json::String::size_type hasSign =
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050039 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
Haibo Huangb0bee822021-02-24 15:40:15 -080040 Json::String::size_type exponentStartIndex = index + 1 + hasSign;
41 Json::String normalized = s.substr(0, exponentStartIndex);
42 Json::String::size_type indexDigit =
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050043 s.find_first_not_of('0', exponentStartIndex);
Haibo Huangb0bee822021-02-24 15:40:15 -080044 Json::String exponent = "0";
45 if (indexDigit != Json::String::npos) // There is an exponent different
46 // from 0
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050047 {
48 exponent = s.substr(indexDigit);
49 }
50 return normalized + exponent;
51 }
52 return s;
53}
54
Haibo Huangb0bee822021-02-24 15:40:15 -080055static Json::String readInputTestFile(const char* path) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050056 FILE* file = fopen(path, "rb");
57 if (!file)
Haibo Huangb0bee822021-02-24 15:40:15 -080058 return "";
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050059 fseek(file, 0, SEEK_END);
Haibo Huangb0bee822021-02-24 15:40:15 -080060 auto const size = ftell(file);
61 auto const usize = static_cast<size_t>(size);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050062 fseek(file, 0, SEEK_SET);
Haibo Huangb0bee822021-02-24 15:40:15 -080063 auto buffer = new char[size + 1];
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050064 buffer[size] = 0;
Haibo Huangb0bee822021-02-24 15:40:15 -080065 Json::String text;
66 if (fread(buffer, 1, usize, file) == usize)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050067 text = buffer;
68 fclose(file);
69 delete[] buffer;
70 return text;
71}
72
Haibo Huangb0bee822021-02-24 15:40:15 -080073static void printValueTree(FILE* fout, Json::Value& value,
74 const Json::String& path = ".") {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050075 if (value.hasComment(Json::commentBefore)) {
76 fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
77 }
78 switch (value.type()) {
79 case Json::nullValue:
80 fprintf(fout, "%s=null\n", path.c_str());
81 break;
82 case Json::intValue:
Haibo Huangb0bee822021-02-24 15:40:15 -080083 fprintf(fout, "%s=%s\n", path.c_str(),
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050084 Json::valueToString(value.asLargestInt()).c_str());
85 break;
86 case Json::uintValue:
Haibo Huangb0bee822021-02-24 15:40:15 -080087 fprintf(fout, "%s=%s\n", path.c_str(),
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050088 Json::valueToString(value.asLargestUInt()).c_str());
89 break;
90 case Json::realValue:
Haibo Huangb0bee822021-02-24 15:40:15 -080091 fprintf(fout, "%s=%s\n", path.c_str(),
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050092 normalizeFloatingPointStr(value.asDouble()).c_str());
93 break;
94 case Json::stringValue:
95 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
96 break;
97 case Json::booleanValue:
98 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
99 break;
100 case Json::arrayValue: {
101 fprintf(fout, "%s=[]\n", path.c_str());
Haibo Huangb0bee822021-02-24 15:40:15 -0800102 Json::ArrayIndex size = value.size();
103 for (Json::ArrayIndex index = 0; index < size; ++index) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500104 static char buffer[16];
Haibo Huangb0bee822021-02-24 15:40:15 -0800105 jsoncpp_snprintf(buffer, sizeof(buffer), "[%u]", index);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500106 printValueTree(fout, value[index], path + buffer);
107 }
108 } break;
109 case Json::objectValue: {
110 fprintf(fout, "%s={}\n", path.c_str());
111 Json::Value::Members members(value.getMemberNames());
112 std::sort(members.begin(), members.end());
Haibo Huangb0bee822021-02-24 15:40:15 -0800113 Json::String suffix = *(path.end() - 1) == '.' ? "" : ".";
114 for (const auto& name : members) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500115 printValueTree(fout, value[name], path + suffix + name);
116 }
117 } break;
118 default:
119 break;
120 }
121
122 if (value.hasComment(Json::commentAfter)) {
123 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
124 }
125}
126
Haibo Huangb0bee822021-02-24 15:40:15 -0800127static int parseAndSaveValueTree(const Json::String& input,
128 const Json::String& actual,
129 const Json::String& kind,
130 const Json::Features& features, bool parseOnly,
131 Json::Value* root, bool use_legacy) {
132 if (!use_legacy) {
133 Json::CharReaderBuilder builder;
134
135 builder.settings_["allowComments"] = features.allowComments_;
136 builder.settings_["strictRoot"] = features.strictRoot_;
137 builder.settings_["allowDroppedNullPlaceholders"] =
138 features.allowDroppedNullPlaceholders_;
139 builder.settings_["allowNumericKeys"] = features.allowNumericKeys_;
140
141 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
142 Json::String errors;
143 const bool parsingSuccessful =
144 reader->parse(input.data(), input.data() + input.size(), root, &errors);
145
146 if (!parsingSuccessful) {
147 std::cerr << "Failed to parse " << kind << " file: " << std::endl
148 << errors << std::endl;
149 return 1;
150 }
151
152 // We may instead check the legacy implementation (to ensure it doesn't
153 // randomly get broken).
154 } else {
155 Json::Reader reader(features);
156 const bool parsingSuccessful =
157 reader.parse(input.data(), input.data() + input.size(), *root);
158 if (!parsingSuccessful) {
159 std::cerr << "Failed to parse " << kind << " file: " << std::endl
160 << reader.getFormatedErrorMessages() << std::endl;
161 return 1;
162 }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500163 }
164
165 if (!parseOnly) {
166 FILE* factual = fopen(actual.c_str(), "wt");
167 if (!factual) {
Haibo Huangb0bee822021-02-24 15:40:15 -0800168 std::cerr << "Failed to create '" << kind << "' actual file."
169 << std::endl;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500170 return 2;
171 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800172 printValueTree(factual, *root);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500173 fclose(factual);
174 }
175 return 0;
176}
Haibo Huangb0bee822021-02-24 15:40:15 -0800177// static Json::String useFastWriter(Json::Value const& root) {
178// Json::FastWriter writer;
179// writer.enableYAMLCompatibility();
180// return writer.write(root);
181// }
182static Json::String useStyledWriter(Json::Value const& root) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500183 Json::StyledWriter writer;
Haibo Huangb0bee822021-02-24 15:40:15 -0800184 return writer.write(root);
185}
186static Json::String useStyledStreamWriter(Json::Value const& root) {
187 Json::StyledStreamWriter writer;
188 Json::OStringStream sout;
189 writer.write(sout, root);
190 return sout.str();
191}
192static Json::String useBuiltStyledStreamWriter(Json::Value const& root) {
193 Json::StreamWriterBuilder builder;
194 return Json::writeString(builder, root);
195}
196static int rewriteValueTree(const Json::String& rewritePath,
197 const Json::Value& root,
198 Options::writeFuncType write,
199 Json::String* rewrite) {
200 *rewrite = write(root);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500201 FILE* fout = fopen(rewritePath.c_str(), "wt");
202 if (!fout) {
Haibo Huangb0bee822021-02-24 15:40:15 -0800203 std::cerr << "Failed to create rewrite file: " << rewritePath << std::endl;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500204 return 2;
205 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800206 fprintf(fout, "%s\n", rewrite->c_str());
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500207 fclose(fout);
208 return 0;
209}
210
Haibo Huangb0bee822021-02-24 15:40:15 -0800211static Json::String removeSuffix(const Json::String& path,
212 const Json::String& extension) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500213 if (extension.length() >= path.length())
Haibo Huangb0bee822021-02-24 15:40:15 -0800214 return Json::String("");
215 Json::String suffix = path.substr(path.length() - extension.length());
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500216 if (suffix != extension)
Haibo Huangb0bee822021-02-24 15:40:15 -0800217 return Json::String("");
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500218 return path.substr(0, path.length() - extension.length());
219}
220
221static void printConfig() {
222// Print the configuration used to compile JsonCpp
223#if defined(JSON_NO_INT64)
Haibo Huangb0bee822021-02-24 15:40:15 -0800224 std::cout << "JSON_NO_INT64=1" << std::endl;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500225#else
Haibo Huangb0bee822021-02-24 15:40:15 -0800226 std::cout << "JSON_NO_INT64=0" << std::endl;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400227#endif
228}
229
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500230static int printUsage(const char* argv[]) {
Haibo Huangb0bee822021-02-24 15:40:15 -0800231 std::cout << "Usage: " << argv[0] << " [--strict] input-json-file"
232 << std::endl;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500233 return 3;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400234}
235
Haibo Huangb0bee822021-02-24 15:40:15 -0800236static int parseCommandLine(int argc, const char* argv[], Options* opts) {
237 opts->parseOnly = false;
238 opts->write = &useStyledWriter;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500239 if (argc < 2) {
240 return printUsage(argv);
241 }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500242 int index = 1;
Haibo Huangb0bee822021-02-24 15:40:15 -0800243 if (Json::String(argv[index]) == "--json-checker") {
244 opts->features = Json::Features::strictMode();
245 opts->parseOnly = true;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500246 ++index;
247 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800248 if (Json::String(argv[index]) == "--json-config") {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500249 printConfig();
250 return 3;
251 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800252 if (Json::String(argv[index]) == "--json-writer") {
253 ++index;
254 Json::String const writerName(argv[index++]);
255 if (writerName == "StyledWriter") {
256 opts->write = &useStyledWriter;
257 } else if (writerName == "StyledStreamWriter") {
258 opts->write = &useStyledStreamWriter;
259 } else if (writerName == "BuiltStyledStreamWriter") {
260 opts->write = &useBuiltStyledStreamWriter;
261 } else {
262 std::cerr << "Unknown '--json-writer' " << writerName << std::endl;
263 return 4;
264 }
265 }
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500266 if (index == argc || index + 1 < argc) {
267 return printUsage(argv);
268 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800269 opts->path = argv[index];
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500270 return 0;
271}
272
Haibo Huangb0bee822021-02-24 15:40:15 -0800273static int runTest(Options const& opts, bool use_legacy) {
274 int exitCode = 0;
275
276 Json::String input = readInputTestFile(opts.path.c_str());
277 if (input.empty()) {
278 std::cerr << "Invalid input file: " << opts.path << std::endl;
279 return 3;
280 }
281
282 Json::String basePath = removeSuffix(opts.path, ".json");
283 if (!opts.parseOnly && basePath.empty()) {
284 std::cerr << "Bad input path '" << opts.path
285 << "'. Must end with '.expected'" << std::endl;
286 return 3;
287 }
288
289 Json::String const actualPath = basePath + ".actual";
290 Json::String const rewritePath = basePath + ".rewrite";
291 Json::String const rewriteActualPath = basePath + ".actual-rewrite";
292
293 Json::Value root;
294 exitCode = parseAndSaveValueTree(input, actualPath, "input", opts.features,
295 opts.parseOnly, &root, use_legacy);
296 if (exitCode || opts.parseOnly) {
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500297 return exitCode;
298 }
299
Haibo Huangb0bee822021-02-24 15:40:15 -0800300 Json::String rewrite;
301 exitCode = rewriteValueTree(rewritePath, root, opts.write, &rewrite);
302 if (exitCode) {
303 return exitCode;
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500304 }
Haibo Huangb0bee822021-02-24 15:40:15 -0800305
306 Json::Value rewriteRoot;
307 exitCode = parseAndSaveValueTree(rewrite, rewriteActualPath, "rewrite",
308 opts.features, opts.parseOnly, &rewriteRoot,
309 use_legacy);
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -0500310
311 return exitCode;
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -0400312}
Haibo Huangb0bee822021-02-24 15:40:15 -0800313
314int main(int argc, const char* argv[]) {
315 Options opts;
316 try {
317 int exitCode = parseCommandLine(argc, argv, &opts);
318 if (exitCode != 0) {
319 std::cerr << "Failed to parse command-line." << std::endl;
320 return exitCode;
321 }
322
323 const int modern_return_code = runTest(opts, false);
324 if (modern_return_code) {
325 return modern_return_code;
326 }
327
328 const std::string filename =
329 opts.path.substr(opts.path.find_last_of("\\/") + 1);
330 const bool should_run_legacy = (filename.rfind("legacy_", 0) == 0);
331 if (should_run_legacy) {
332 return runTest(opts, true);
333 }
334 } catch (const std::exception& e) {
335 std::cerr << "Unhandled exception:" << std::endl << e.what() << std::endl;
336 return 1;
337 }
338}
339
340#if defined(__GNUC__)
341#pragma GCC diagnostic pop
342#endif