simdjson/tools/json2json.cpp

89 lines
2.7 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"
2020-06-24 04:44:25 +08:00
SIMDJSON_PUSH_DISABLE_ALL_WARNINGS
#ifndef __cpp_exceptions
#define CXXOPTS_NO_EXCEPTIONS
#endif
#include "cxxopts.hpp"
2020-06-24 04:44:25 +08:00
SIMDJSON_POP_DISABLE_WARNINGS
2018-12-07 06:22:22 +08:00
2020-11-04 04:48:09 +08:00
#if CXXOPTS__VERSION_MAJOR < 3
2018-12-07 06:22:22 +08:00
int main(int argc, char *argv[]) {
#else
int main(int argc, const char *argv[]) {
#endif
#ifdef __cpp_exceptions
try {
#endif
std::string progName = "json2json";
std::string progUsage = "json2json version ";
progUsage += STRINGIFY(SIMDJSON_VERSION);
progUsage += " (";
progUsage += simdjson::active_implementation->name();
progUsage += ")\n";
progUsage += "Reads json in, out the result of the parsing.\n";
progUsage += argv[0];
progUsage += " <jsonfile>\n";
cxxopts::Options options(progName, progUsage);
options.add_options()
("z,ondemand", "Use On Demand front-end.", cxxopts::value<bool>()->default_value("false"))
("d,rawdump", "Dumps the raw content of the tape.", cxxopts::value<bool>()->default_value("false"))
("f,file", "File name.", cxxopts::value<std::string>())
("h,help", "Print usage.")
;
// the first argument without an option name will be parsed into file
options.parse_positional({"file"});
auto result = options.parse(argc, argv);
if(result.count("help")) {
std::cerr << options.help() << std::endl;
return EXIT_SUCCESS;
}
bool ondemand = result["ondemand"].as<bool>();
bool rawdump = result["rawdump"].as<bool>();
if(!result.count("file")) {
std::cerr << "No filename specified." << std::endl;
std::cerr << options.help() << std::endl;
2020-04-19 07:37:11 +08:00
return EXIT_FAILURE;
2018-12-07 06:22:22 +08:00
}
const char *filename = result["file"].as<std::string>().c_str();
if(ondemand) {
simdjson::ondemand::parser parser;
simdjson::padded_string docdata;
auto error = simdjson::padded_string::load(filename).get(docdata);
if(error != simdjson::SUCCESS) { std::cout << error << std::endl; return EXIT_FAILURE; }
simdjson::ondemand::document doc;
error = parser.iterate(docdata).get(doc);
if(error != simdjson::SUCCESS) { std::cout << error << std::endl; return EXIT_FAILURE; }
std::cout << doc;
return EXIT_SUCCESS;
}
2020-03-29 02:43:41 +08:00
simdjson::dom::parser parser;
simdjson::dom::element doc;
auto error = parser.load(filename).get(doc); // do the parsing, return false on error
if (error) {
std::cerr << " Parsing failed. Error is '" << 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;
#ifdef __cpp_exceptions
} catch (const cxxopts::OptionException& e) {
std::cout << "error parsing options: " << e.what() << std::endl;
return EXIT_FAILURE;
}
#endif
2018-12-07 06:22:22 +08:00
}