simdjson/tools/json2json.cpp

51 lines
1.2 KiB
C++
Raw Normal View History

2018-12-10 02:08:41 +08:00
#include <iostream>
#ifndef _MSC_VER
2018-12-07 06:22:22 +08:00
#include <unistd.h>
#endif
#include "simdjson.h"
2018-12-07 06:22:22 +08:00
int main(int argc, char *argv[]) {
bool rawdump = false;
#ifndef _MSC_VER
2018-12-07 06:22:22 +08:00
int c;
while ((c = getopt(argc, argv, "d")) != -1) {
2018-12-10 02:08:41 +08:00
switch (c) {
case 'd':
rawdump = true;
break;
default:
abort();
}
}
#else
int optind = 1;
#endif
2018-12-07 06:22:22 +08:00
if (optind >= argc) {
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) {
std::cerr << "warning: ignoring everything after " << argv[optind + 1]
<< std::endl;
2018-12-07 06:22:22 +08:00
}
simdjson::document::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;
}