2018-12-10 02:08:41 +08:00
|
|
|
#include <iostream>
|
2018-12-31 10:00:19 +08:00
|
|
|
#ifndef _MSC_VER
|
2018-12-07 06:22:22 +08:00
|
|
|
#include <unistd.h>
|
2018-12-31 10:00:19 +08:00
|
|
|
#endif
|
2018-12-07 06:22:22 +08:00
|
|
|
#include "simdjson/jsonioutil.h"
|
2018-12-10 02:08:41 +08:00
|
|
|
#include "simdjson/jsonparser.h"
|
2018-12-07 06:22:22 +08:00
|
|
|
|
2019-07-31 05:18:10 +08:00
|
|
|
void compute_dump(simdjson::ParsedJson::Iterator &pjh) {
|
2018-12-15 10:32:42 +08:00
|
|
|
if (pjh.is_object()) {
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cout << "{";
|
2018-12-15 10:32:42 +08:00
|
|
|
if (pjh.down()) {
|
|
|
|
pjh.print(std::cout); // must be a string
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cout << ":";
|
2018-12-15 10:32:42 +08:00
|
|
|
pjh.next();
|
2018-12-10 02:08:41 +08:00
|
|
|
compute_dump(pjh); // let us recurse
|
2018-12-15 10:32:42 +08:00
|
|
|
while (pjh.next()) {
|
|
|
|
std::cout << ",";
|
|
|
|
pjh.print(std::cout);
|
|
|
|
std::cout << ":";
|
|
|
|
pjh.next();
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
}
|
|
|
|
pjh.up();
|
2018-12-08 12:35:53 +08:00
|
|
|
}
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cout << "}";
|
2018-12-15 10:32:42 +08:00
|
|
|
} else if (pjh.is_array()) {
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cout << "[";
|
2018-12-15 10:32:42 +08:00
|
|
|
if (pjh.down()) {
|
2018-12-10 02:08:41 +08:00
|
|
|
compute_dump(pjh); // let us recurse
|
2018-12-15 10:32:42 +08:00
|
|
|
while (pjh.next()) {
|
|
|
|
std::cout << ",";
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
}
|
|
|
|
pjh.up();
|
2018-12-08 12:35:53 +08:00
|
|
|
}
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cout << "]";
|
2018-12-15 10:32:42 +08:00
|
|
|
} else {
|
|
|
|
pjh.print(std::cout); // just print the lone value
|
2018-12-10 02:08:41 +08:00
|
|
|
}
|
2018-12-08 12:35:53 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 06:22:22 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2019-07-31 05:18:10 +08:00
|
|
|
bool rawdump = false;
|
|
|
|
bool apidump = false;
|
2018-12-31 10:00:19 +08:00
|
|
|
|
|
|
|
#ifndef _MSC_VER
|
2018-12-07 06:22:22 +08:00
|
|
|
int c;
|
|
|
|
|
2019-02-24 00:28:20 +08:00
|
|
|
while ((c = getopt(argc, argv, "da")) != -1) {
|
2018-12-10 02:08:41 +08:00
|
|
|
switch (c) {
|
|
|
|
case 'd':
|
|
|
|
rawdump = true;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
apidump = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
2019-07-31 05:18:10 +08:00
|
|
|
}
|
2018-12-31 10:00:19 +08:00
|
|
|
#else
|
|
|
|
int optind = 1;
|
|
|
|
#endif
|
2018-12-07 06:22:22 +08:00
|
|
|
if (optind >= argc) {
|
2019-05-10 05:59:51 +08:00
|
|
|
std::cerr << "Reads json in, out the result of the parsing. " << std::endl;
|
|
|
|
std::cerr << "Usage: " << argv[0] << " <jsonfile>" << std::endl;
|
|
|
|
std::cerr << "The -d flag dumps the raw content of the tape." << std::endl;
|
2018-12-13 11:42:19 +08:00
|
|
|
|
2018-12-07 06:22:22 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2018-12-10 02:08:41 +08:00
|
|
|
const char *filename = argv[optind];
|
|
|
|
if (optind + 1 < argc) {
|
2019-07-31 05:18:10 +08:00
|
|
|
std::cerr << "warning: ignoring everything after " << argv[optind + 1]
|
|
|
|
<< std::endl;
|
2018-12-07 06:22:22 +08:00
|
|
|
}
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::padded_string p;
|
2018-12-07 06:22:22 +08:00
|
|
|
try {
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::get_corpus(filename).swap(p);
|
2019-08-01 05:43:45 +08:00
|
|
|
} catch (const std::exception &) { // caught by reference to base
|
2018-12-07 06:22:22 +08:00
|
|
|
std::cout << "Could not load the file " << filename << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-07-03 03:21:00 +08:00
|
|
|
simdjson::ParsedJson pj;
|
2019-07-31 05:18:10 +08:00
|
|
|
bool allocok = pj.allocate_capacity(p.size(), 1024);
|
2018-12-10 02:08:41 +08:00
|
|
|
if (!allocok) {
|
2018-12-07 06:22:22 +08:00
|
|
|
std::cerr << "failed to allocate memory" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-07-31 05:18:10 +08:00
|
|
|
int res =
|
|
|
|
simdjson::json_parse(p, pj); // do the parsing, return false on error
|
2019-07-29 22:07:07 +08:00
|
|
|
if (res != simdjson::SUCCESS) {
|
2019-07-31 05:18:10 +08:00
|
|
|
std::cerr << " Parsing failed. Error is '" << simdjson::error_message(res)
|
|
|
|
<< "'." << std::endl;
|
2018-12-07 06:22:22 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2018-12-10 02:08:41 +08:00
|
|
|
if (apidump) {
|
2019-07-31 05:18:10 +08:00
|
|
|
simdjson::ParsedJson::Iterator pjh(pj);
|
|
|
|
if (!pjh.is_ok()) {
|
2018-12-10 02:08:41 +08:00
|
|
|
std::cerr << " Could not iterate parsed result. " << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
compute_dump(pjh);
|
2018-12-08 12:35:53 +08:00
|
|
|
} else {
|
2019-07-31 05:18:10 +08:00
|
|
|
const bool is_ok =
|
|
|
|
rawdump ? pj.dump_raw_tape(std::cout) : pj.print_json(std::cout);
|
2018-12-10 02:08:41 +08:00
|
|
|
if (!is_ok) {
|
2018-12-08 12:35:53 +08:00
|
|
|
std::cerr << " Could not print out parsed result. " << std::endl;
|
2018-12-10 02:08:41 +08:00
|
|
|
return EXIT_FAILURE;
|
2018-12-08 12:35:53 +08:00
|
|
|
}
|
2018-12-07 06:22:22 +08:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|