2020-03-03 06:23:19 +08:00
|
|
|
#include "simdjson.h"
|
2019-07-31 05:18:10 +08:00
|
|
|
#include <iostream>
|
2019-07-27 06:38:10 +08:00
|
|
|
|
2019-07-31 05:18:10 +08:00
|
|
|
void compute_dump(simdjson::ParsedJson::Iterator &pjh) {
|
2019-07-27 06:38:10 +08:00
|
|
|
if (pjh.is_object()) {
|
|
|
|
std::cout << "{";
|
|
|
|
if (pjh.down()) {
|
|
|
|
pjh.print(std::cout); // must be a string
|
|
|
|
std::cout << ":";
|
|
|
|
pjh.next();
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
while (pjh.next()) {
|
|
|
|
std::cout << ",";
|
|
|
|
pjh.print(std::cout);
|
|
|
|
std::cout << ":";
|
|
|
|
pjh.next();
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
}
|
|
|
|
pjh.up();
|
|
|
|
}
|
|
|
|
std::cout << "}";
|
|
|
|
} else if (pjh.is_array()) {
|
|
|
|
std::cout << "[";
|
|
|
|
if (pjh.down()) {
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
while (pjh.next()) {
|
|
|
|
std::cout << ",";
|
|
|
|
compute_dump(pjh); // let us recurse
|
|
|
|
}
|
|
|
|
pjh.up();
|
|
|
|
}
|
|
|
|
std::cout << "]";
|
|
|
|
} else {
|
|
|
|
pjh.print(std::cout); // just print the lone value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc < 3) {
|
|
|
|
std::cerr << "Usage: " << argv[0] << " <jsonfile> <jsonpath>" << std::endl;
|
2019-07-31 05:18:10 +08:00
|
|
|
std::cerr << "Follows the rfc6901 standard's syntax: "
|
|
|
|
"https://tools.ietf.org/html/rfc6901"
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << " Example: " << argv[0]
|
|
|
|
<< " jsonexamples/small/demo.json /Image/Width /Image/Height "
|
|
|
|
"/Image/IDs/2 "
|
|
|
|
<< std::endl;
|
|
|
|
std::cerr << "Multiple <jsonpath> can be issued in the same command, but "
|
|
|
|
"at least one is needed."
|
|
|
|
<< std::endl;
|
2019-07-27 06:38:10 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2020-03-25 00:47:33 +08:00
|
|
|
|
2019-07-27 06:38:10 +08:00
|
|
|
const char *filename = argv[1];
|
2020-03-25 00:47:33 +08:00
|
|
|
auto [doc, error] = simdjson::document::load(filename);
|
|
|
|
if (error) { std::cerr << "Error parsing " << filename << ": " << error << std::endl; }
|
|
|
|
|
2019-07-27 06:38:10 +08:00
|
|
|
std::cout << "[" << std::endl;
|
2019-07-31 05:18:10 +08:00
|
|
|
for (int idx = 2; idx < argc; idx++) {
|
2020-03-25 00:47:33 +08:00
|
|
|
const char *json_pointer = argv[idx];
|
|
|
|
auto [value, pointer_error] = doc[json_pointer];
|
|
|
|
std::cout << "{\"jsonpath\": \"" << json_pointer << "\"";
|
|
|
|
if (pointer_error) {
|
|
|
|
std::cout << ",\"error\":\"" << pointer_error << "\"";
|
2019-07-31 05:18:10 +08:00
|
|
|
} else {
|
2020-03-25 00:47:33 +08:00
|
|
|
std::cout << ",\"value\":" << value;
|
2019-07-31 05:18:10 +08:00
|
|
|
}
|
2020-03-25 00:47:33 +08:00
|
|
|
std::cout << "}";
|
2019-07-31 05:18:10 +08:00
|
|
|
if (idx + 1 < argc) {
|
2020-03-25 00:47:33 +08:00
|
|
|
std::cout << ",";
|
2019-07-31 05:18:10 +08:00
|
|
|
}
|
2020-03-25 00:47:33 +08:00
|
|
|
std::cout << std::endl;
|
2019-07-27 06:38:10 +08:00
|
|
|
}
|
|
|
|
std::cout << "]" << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|