simdjson/tools/json2json.cpp

117 lines
3.0 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/jsonioutil.h"
2018-12-10 02:08:41 +08:00
#include "simdjson/jsonparser.h"
2018-12-07 06:22:22 +08:00
using namespace std;
2018-12-10 02:08:41 +08:00
void compute_dump(ParsedJson::iterator &pjh) {
bool inobject = (pjh.get_type() == '{');
bool inarray = (pjh.get_type() == '[');
if ((!inobject) && (!inarray)) {
pjh.print(std::cout); // just print the lone value
return; // we are done
}
// we have either an array or an object
2018-12-10 09:47:02 +08:00
bool goingdown = pjh.down();
if(!goingdown) {
// we have an empty scope
if(inobject) std::cout<<"{}";
else std::cout<<"[]";
return;
}
// we have a non-empty scope and we are at the beginning of it
2018-12-10 02:08:41 +08:00
if (inobject) {
2018-12-12 11:39:39 +08:00
assert(pjh.get_scope_type() == '{');
2018-12-10 02:08:41 +08:00
std::cout << "{";
assert(pjh.get_type() == '"');
pjh.print(std::cout); // must be a string
std::cout << ":";
assert(pjh.next());
compute_dump(pjh); // let us recurse
while (pjh.next()) {
std::cout << ",";
assert(pjh.get_type() == '"');
pjh.print(std::cout);
std::cout << ":";
assert(pjh.next());
compute_dump(pjh); // let us recurse
}
2018-12-10 02:08:41 +08:00
std::cout << "}";
} else {
2018-12-12 11:39:39 +08:00
assert(pjh.get_scope_type() == '[');
2018-12-10 02:08:41 +08:00
std::cout << "[";
compute_dump(pjh); // let us recurse
while (pjh.next()) {
std::cout << ",";
compute_dump(pjh); // let us recurse
}
2018-12-10 02:08:41 +08:00
std::cout << "]";
}
2018-12-10 09:47:02 +08:00
assert(pjh.up());
}
2018-12-07 06:22:22 +08:00
int main(int argc, char *argv[]) {
int c;
bool rawdump = false;
bool apidump = false;
2018-12-07 06:22:22 +08:00
2018-12-10 02:08:41 +08:00
while ((c = getopt(argc, argv, "da")) != -1)
switch (c) {
case 'd':
rawdump = true;
break;
case 'a':
apidump = true;
break;
default:
abort();
}
2018-12-07 06:22:22 +08:00
if (optind >= argc) {
2018-12-10 02:08:41 +08:00
cerr << "Reads json in, out the result of the parsing. " << endl;
2018-12-07 06:22:22 +08:00
cerr << "Usage: " << argv[0] << " <jsonfile>" << endl;
2018-12-13 11:42:19 +08:00
cerr << "The -d flag dumps the raw content of the tape." << endl;
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) {
cerr << "warning: ignoring everything after " << argv[optind + 1] << endl;
2018-12-07 06:22:22 +08:00
}
std::string_view p;
try {
p = get_corpus(filename);
2018-12-10 02:08:41 +08:00
} catch (const std::exception &e) { // 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;
}
ParsedJson pj;
bool allocok = pj.allocateCapacity(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;
}
bool is_ok = json_parse(p, pj); // do the parsing, return false on error
2018-12-12 06:20:29 +08:00
free((void*)p.data());
2018-12-07 06:22:22 +08:00
if (!is_ok) {
std::cerr << " Parsing failed. " << std::endl;
return EXIT_FAILURE;
}
2018-12-10 02:08:41 +08:00
if (apidump) {
ParsedJson::iterator pjh(pj);
if (!pjh.isOk()) {
std::cerr << " Could not iterate parsed result. " << std::endl;
return EXIT_FAILURE;
}
compute_dump(pjh);
} else {
2018-12-11 04:16:31 +08:00
is_ok = rawdump ? pj.dump_raw_tape(std::cout) : pj.printjson(std::cout);
2018-12-10 02:08:41 +08:00
if (!is_ok) {
std::cerr << " Could not print out parsed result. " << std::endl;
2018-12-10 02:08:41 +08:00
return EXIT_FAILURE;
}
2018-12-07 06:22:22 +08:00
}
return EXIT_SUCCESS;
}