simdjson/tools/json2json.cpp

52 lines
1.4 KiB
C++
Raw Normal View History

2018-12-10 02:08:41 +08:00
#include <iostream>
2018-12-07 06:22:22 +08:00
#include <unistd.h>
#include "simdjson.h"
2018-12-07 06:22:22 +08:00
2020-04-19 07:37:11 +08:00
void usage(const char *exe) {
std::cerr << exe << " v" << STRINGIFY(SIMDJSON_VERSION) << " (" << simdjson::active_implementation->name() << ")" << std::endl;
std::cerr << std::endl;
std::cerr << "Reads json in, out the result of the parsing. " << std::endl;
std::cerr << "Usage: " << exe << " <jsonfile>" << std::endl;
std::cerr << "The -d flag dumps the raw content of the tape." << std::endl;
}
2018-12-07 06:22:22 +08:00
int main(int argc, char *argv[]) {
bool rawdump = false;
2018-12-07 06:22:22 +08:00
int c;
2020-04-19 07:37:11 +08:00
while ((c = getopt(argc, argv, "dh")) != -1) {
2018-12-10 02:08:41 +08:00
switch (c) {
case 'd':
rawdump = true;
break;
2020-04-19 07:37:11 +08:00
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
2018-12-10 02:08:41 +08:00
default:
abort();
}
}
2018-12-07 06:22:22 +08:00
if (optind >= argc) {
2020-04-19 07:37:11 +08:00
usage(argv[0]);
return EXIT_FAILURE;
2018-12-07 06:22:22 +08:00
}
2018-12-10 02:08:41 +08:00
const char *filename = argv[optind];
if (optind + 1 < argc) {
std::cerr << "warning: ignoring everything after " << argv[optind + 1]
<< std::endl;
2018-12-07 06:22:22 +08:00
}
2020-03-29 02:43:41 +08:00
simdjson::dom::parser parser;
auto [doc, error] = parser.load(filename); // do the parsing, return false on error
if (error != simdjson::SUCCESS) {
std::cerr << " Parsing failed. Error is '" << simdjson::error_message(error)
<< "'." << std::endl;
2018-12-07 06:22:22 +08:00
return EXIT_FAILURE;
}
if(rawdump) {
doc.dump_raw_tape(std::cout);
} else {
std::cout << doc;
2018-12-07 06:22:22 +08:00
}
return EXIT_SUCCESS;
}