2020-06-13 05:57:45 +08:00
|
|
|
#include "simdjson.cpp"
|
2021-08-08 00:43:40 +08:00
|
|
|
#include "simdjson.h"
|
|
|
|
#include <iostream>
|
2020-07-25 18:02:19 +08:00
|
|
|
|
2020-06-13 05:57:45 +08:00
|
|
|
int main(int argc, char *argv[]) {
|
2021-08-08 00:43:40 +08:00
|
|
|
if (argc < 2) {
|
|
|
|
std::cerr << "Please specify at least one file name and" << std::endl;
|
|
|
|
std::cerr << "up to two files." << std::endl;
|
|
|
|
std::cerr << "The first file should be a JSON document." << std::endl;
|
|
|
|
std::cerr << "The secod file should container many JSON documents."
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << "Try the test files: jsonexamples/twitter.json "
|
|
|
|
"jsonexamples/amazon_cellphones.ndjson"
|
|
|
|
<< std::endl;
|
2020-06-24 10:07:40 +08:00
|
|
|
return EXIT_FAILURE;
|
2020-06-13 05:57:45 +08:00
|
|
|
}
|
2021-08-08 00:43:40 +08:00
|
|
|
const char *filename = argv[1];
|
|
|
|
|
|
|
|
simdjson::padded_string json;
|
|
|
|
std::cout << "loading: " << filename << std::endl;
|
|
|
|
auto error = simdjson::padded_string::load(filename).get(json);
|
2020-06-13 05:57:45 +08:00
|
|
|
if (error) {
|
2021-08-08 00:43:40 +08:00
|
|
|
std::cout << "could not load the file " << filename << std::endl;
|
2020-06-13 05:57:45 +08:00
|
|
|
std::cout << "error code: " << error << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
2021-08-08 00:43:40 +08:00
|
|
|
std::cout << "loaded: " << json.size() << " bytes." << std::endl;
|
2020-06-13 05:57:45 +08:00
|
|
|
}
|
2021-08-08 00:43:40 +08:00
|
|
|
simdjson::ondemand::parser parser;
|
|
|
|
simdjson::ondemand::document doc;
|
|
|
|
error = parser.iterate(json).get(doc);
|
|
|
|
if (error) {
|
|
|
|
std::cout << error << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
simdjson::ondemand::json_type type;
|
|
|
|
error = doc.type().get(type);
|
|
|
|
if (error) {
|
|
|
|
std::cout << error << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
std::cout << "document has the following type at the root: " << type
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
if (argc == 2) {
|
2020-06-13 05:57:45 +08:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2021-08-08 00:43:40 +08:00
|
|
|
// iterate_many
|
|
|
|
const char *filename2 = argv[2];
|
|
|
|
std::cout << "loading: " << filename2 << std::endl;
|
|
|
|
simdjson::padded_string json2;
|
|
|
|
error = simdjson::padded_string::load(filename2).get(json2);
|
|
|
|
if (error) {
|
|
|
|
std::cout << "could not load the file " << filename2 << std::endl;
|
|
|
|
std::cout << "error code: " << error << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
std::cout << "loaded: " << json2.size() << " bytes." << std::endl;
|
|
|
|
}
|
|
|
|
simdjson::ondemand::document_stream stream;
|
|
|
|
error = parser.iterate_many(json2).get(stream);
|
|
|
|
size_t counter{0};
|
2020-06-21 13:03:57 +08:00
|
|
|
if (!error) {
|
|
|
|
for (auto result : stream) {
|
|
|
|
error = result.error();
|
2021-08-08 00:43:40 +08:00
|
|
|
counter++;
|
2020-06-21 13:03:57 +08:00
|
|
|
}
|
2020-06-13 05:57:45 +08:00
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
std::cout << "parse_many failed" << std::endl;
|
|
|
|
std::cout << "error code: " << error << std::endl;
|
|
|
|
std::cout << error << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
2021-08-08 00:43:40 +08:00
|
|
|
std::cout << "iterate_many valid" << std::endl;
|
|
|
|
std::cout << "found " << counter << " documents" << std::endl;
|
2020-06-13 05:57:45 +08:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|