blob: 0b2f8e24a94fb138e1ac6953760dd42d87752faf [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
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100018static std::string normalizeFloatingPointStr(double value) {
19 char buffer[32];
Aaron Jacobsd2618802013-08-08 23:08:28 +000020#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100021 sprintf_s(buffer, sizeof(buffer), "%.16g", value);
Aaron Jacobsd2618802013-08-08 23:08:28 +000022#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100023 snprintf(buffer, sizeof(buffer), "%.16g", value);
Aaron Jacobsd2618802013-08-08 23:08:28 +000024#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100025 buffer[sizeof(buffer) - 1] = 0;
26 std::string s(buffer);
27 std::string::size_type index = s.find_last_of("eE");
28 if (index != std::string::npos) {
29 std::string::size_type hasSign =
30 (s[index + 1] == '+' || s[index + 1] == '-') ? 1 : 0;
31 std::string::size_type exponentStartIndex = index + 1 + hasSign;
32 std::string normalized = s.substr(0, exponentStartIndex);
33 std::string::size_type indexDigit =
34 s.find_first_not_of('0', exponentStartIndex);
35 std::string exponent = "0";
36 if (indexDigit !=
37 std::string::npos) // There is an exponent different from 0
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000038 {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100039 exponent = s.substr(indexDigit);
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000040 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100041 return normalized + exponent;
42 }
43 return s;
Baptiste Lepilleurf0b24e72011-05-26 20:14:32 +000044}
45
Aaron Jacobs11086dd2014-09-15 10:15:29 +100046static std::string readInputTestFile(const char* path) {
47 FILE* file = fopen(path, "rb");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 if (!file)
49 return std::string("");
50 fseek(file, 0, SEEK_END);
51 long size = ftell(file);
52 fseek(file, 0, SEEK_SET);
53 std::string text;
Aaron Jacobs11086dd2014-09-15 10:15:29 +100054 char* buffer = new char[size + 1];
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100055 buffer[size] = 0;
56 if (fread(buffer, 1, size, file) == (unsigned long)size)
57 text = buffer;
58 fclose(file);
59 delete[] buffer;
60 return text;
Christopher Dunnf9864232007-06-14 21:01:26 +000061}
62
Christopher Dunnf9864232007-06-14 21:01:26 +000063static void
Aaron Jacobs11086dd2014-09-15 10:15:29 +100064printValueTree(FILE* fout, Json::Value& value, const std::string& path = ".") {
Cory Quammen4d234922014-10-09 16:29:47 -040065 if (value.hasComment(Json::commentBefore)) {
66 fprintf(fout, "%s\n", value.getComment(Json::commentBefore).c_str());
67 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100068 switch (value.type()) {
69 case Json::nullValue:
70 fprintf(fout, "%s=null\n", path.c_str());
71 break;
72 case Json::intValue:
73 fprintf(fout,
74 "%s=%s\n",
75 path.c_str(),
76 Json::valueToString(value.asLargestInt()).c_str());
77 break;
78 case Json::uintValue:
79 fprintf(fout,
80 "%s=%s\n",
81 path.c_str(),
82 Json::valueToString(value.asLargestUInt()).c_str());
83 break;
84 case Json::realValue:
85 fprintf(fout,
86 "%s=%s\n",
87 path.c_str(),
88 normalizeFloatingPointStr(value.asDouble()).c_str());
89 break;
90 case Json::stringValue:
91 fprintf(fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str());
92 break;
93 case Json::booleanValue:
94 fprintf(fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false");
95 break;
96 case Json::arrayValue: {
97 fprintf(fout, "%s=[]\n", path.c_str());
98 int size = value.size();
99 for (int index = 0; index < size; ++index) {
100 static char buffer[16];
Aaron Jacobsd2618802013-08-08 23:08:28 +0000101#if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000102 sprintf_s(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000103#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000104 snprintf(buffer, sizeof(buffer), "[%d]", index);
Aaron Jacobsd2618802013-08-08 23:08:28 +0000105#endif
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000106 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());
113 std::string suffix = *(path.end() - 1) == '.' ? "" : ".";
114 for (Json::Value::Members::iterator it = members.begin();
115 it != members.end();
116 ++it) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000117 const std::string& name = *it;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000118 printValueTree(fout, value[name], path + suffix + name);
119 }
120 } break;
121 default:
122 break;
123 }
Cory Quammen4d234922014-10-09 16:29:47 -0400124
125 if (value.hasComment(Json::commentAfter)) {
126 fprintf(fout, "%s\n", value.getComment(Json::commentAfter).c_str());
127 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000128}
129
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000130static int parseAndSaveValueTree(const std::string& input,
131 const std::string& actual,
132 const std::string& kind,
133 Json::Value& root,
134 const Json::Features& features,
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000135 bool parseOnly) {
136 Json::Reader reader(features);
137 bool parsingSuccessful = reader.parse(input, root);
138 if (!parsingSuccessful) {
139 printf("Failed to parse %s file: \n%s\n",
140 kind.c_str(),
141 reader.getFormattedErrorMessages().c_str());
142 return 1;
143 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000144
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000145 if (!parseOnly) {
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000146 FILE* factual = fopen(actual.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000147 if (!factual) {
148 printf("Failed to create %s actual file.\n", kind.c_str());
Christopher Dunnf9864232007-06-14 21:01:26 +0000149 return 2;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000150 }
151 printValueTree(factual, root);
152 fclose(factual);
153 }
154 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000155}
156
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000157static int rewriteValueTree(const std::string& rewritePath,
158 const Json::Value& root,
159 std::string& rewrite) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000160 // Json::FastWriter writer;
161 // writer.enableYAMLCompatibility();
Christopher Dunn2160c9a2015-01-23 09:02:44 -0600162 Json::StyledStreamWriter writer;
163 std::ostringstream sout;
164 writer.write(sout, root);
165 rewrite = sout.str();
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000166 FILE* fout = fopen(rewritePath.c_str(), "wt");
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000167 if (!fout) {
168 printf("Failed to create rewrite file: %s\n", rewritePath.c_str());
169 return 2;
170 }
171 fprintf(fout, "%s\n", rewrite.c_str());
172 fclose(fout);
173 return 0;
Christopher Dunnf9864232007-06-14 21:01:26 +0000174}
175
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000176static std::string removeSuffix(const std::string& path,
177 const std::string& extension) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000178 if (extension.length() >= path.length())
179 return std::string("");
180 std::string suffix = path.substr(path.length() - extension.length());
181 if (suffix != extension)
182 return std::string("");
183 return path.substr(0, path.length() - extension.length());
184}
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000185
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000186static void printConfig() {
187// Print the configuration used to compile JsonCpp
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000188#if defined(JSON_NO_INT64)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000189 printf("JSON_NO_INT64=1\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000190#else
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000191 printf("JSON_NO_INT64=0\n");
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000192#endif
193}
194
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000195static int printUsage(const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000196 printf("Usage: %s [--strict] input-json-file", argv[0]);
197 return 3;
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000198}
199
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000200int parseCommandLine(int argc,
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000201 const char* argv[],
202 Json::Features& features,
203 std::string& path,
204 bool& parseOnly) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000205 parseOnly = false;
206 if (argc < 2) {
207 return printUsage(argv);
208 }
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000209
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000210 int index = 1;
211 if (std::string(argv[1]) == "--json-checker") {
212 features = Json::Features::strictMode();
213 parseOnly = true;
214 ++index;
215 }
Christopher Dunnf9864232007-06-14 21:01:26 +0000216
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000217 if (std::string(argv[1]) == "--json-config") {
218 printConfig();
219 return 3;
220 }
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000221
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000222 if (index == argc || index + 1 < argc) {
223 return printUsage(argv);
224 }
225
226 path = argv[index];
227 return 0;
228}
229
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000230int main(int argc, const char* argv[]) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000231 std::string path;
232 Json::Features features;
233 bool parseOnly;
234 int exitCode = parseCommandLine(argc, argv, features, path, parseOnly);
235 if (exitCode != 0) {
236 return exitCode;
237 }
238
239 try {
240 std::string input = readInputTestFile(path.c_str());
241 if (input.empty()) {
242 printf("Failed to read input or empty input: %s\n", path.c_str());
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000243 return 3;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000244 }
Baptiste Lepilleur201fb2c2010-04-19 07:37:41 +0000245
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000246 std::string basePath = removeSuffix(argv[1], ".json");
247 if (!parseOnly && basePath.empty()) {
248 printf("Bad input path. Path does not end with '.expected':\n%s\n",
249 path.c_str());
250 return 3;
251 }
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000252
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000253 std::string actualPath = basePath + ".actual";
254 std::string rewritePath = basePath + ".rewrite";
255 std::string rewriteActualPath = basePath + ".actual-rewrite";
Baptiste Lepilleur88681472009-11-18 21:38:54 +0000256
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000257 Json::Value root;
258 exitCode = parseAndSaveValueTree(
259 input, actualPath, "input", root, features, parseOnly);
Christopher Dunn05810a72015-01-23 11:06:27 -0600260 if (exitCode || parseOnly) {
Christopher Dunn942e2c92015-01-23 11:03:55 -0600261 return exitCode;
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000262 }
Christopher Dunn942e2c92015-01-23 11:03:55 -0600263 std::string rewrite;
264 exitCode = rewriteValueTree(rewritePath, root, rewrite);
Christopher Dunn05810a72015-01-23 11:06:27 -0600265 if (exitCode) {
Christopher Dunn942e2c92015-01-23 11:03:55 -0600266 return exitCode;
267 }
268 Json::Value rewriteRoot;
Christopher Dunn05810a72015-01-23 11:06:27 -0600269 exitCode = parseAndSaveValueTree(
270 rewrite, rewriteActualPath, "rewrite", rewriteRoot, features, parseOnly);
271 if (exitCode) {
272 return exitCode;
273 }
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000274 }
Aaron Jacobs11086dd2014-09-15 10:15:29 +1000275 catch (const std::exception& e) {
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000276 printf("Unhandled exception:\n%s\n", e.what());
277 exitCode = 1;
278 }
Baptiste Lepilleur842e9ac2010-12-27 17:45:23 +0000279
Aaron Jacobs9fa4e842014-07-01 08:48:54 +1000280 return exitCode;
Christopher Dunnf9864232007-06-14 21:01:26 +0000281}