simdjson/tools/jsonpointer.cpp

45 lines
1.4 KiB
C++
Raw Normal View History

#include "simdjson.h"
#include <iostream>
int main(int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <jsonfile> <jsonpath>" << std::endl;
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;
exit(1);
}
2020-03-25 00:47:33 +08:00
const char *filename = argv[1];
2020-03-29 02:43:41 +08:00
simdjson::dom::parser parser;
auto [doc, error] = parser.load(filename);
2020-03-25 00:47:33 +08:00
if (error) { std::cerr << "Error parsing " << filename << ": " << error << std::endl; }
std::cout << "[" << std::endl;
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 << "\"";
} else {
2020-03-25 00:47:33 +08:00
std::cout << ",\"value\":" << value;
}
2020-03-25 00:47:33 +08:00
std::cout << "}";
if (idx + 1 < argc) {
2020-03-25 00:47:33 +08:00
std::cout << ",";
}
2020-03-25 00:47:33 +08:00
std::cout << std::endl;
}
std::cout << "]" << std::endl;
return EXIT_SUCCESS;
}