Skip to content
Snippets Groups Projects
Commit f9759106 authored by Bensong Liu's avatar Bensong Liu
Browse files

implemented path resolve

parent a4bbf77d
No related branches found
No related tags found
No related merge requests found
.ccls-cache .ccls-cache
json2table
CXX ?= g++ -O3 CXX ?= g++
build: build:
$(CXX) json2table.cc -std=c++17 lib/fort.c -o json2table $(CXX) -O3 json2table.cc -std=c++17 lib/fort.c -o json2table
install: build
cp json2table /usr/bin/
...@@ -27,6 +27,31 @@ inline string json_to_string(json j) { ...@@ -27,6 +27,31 @@ inline string json_to_string(json j) {
return "UNKNOWN"; return "UNKNOWN";
} }
void naive_json_access_path(json &input, rlib::string json_path) {
for(auto &next : json_path.split('/')) {
if(!next.empty()) {
if(input.is_object()) {
// Simplest case.
input = input[next];
}
else if(input.is_array()) {
// Do this for every element.
json result_json_arr = json::array();
for(auto &[_, item] : input.items()) {
if(item.is_object())
result_json_arr.push_back(item[next]);
else
throw std::invalid_argument("json_path is not valid for json. No element `" + next + "` found in json input. (note that I support only one-level array iterate)");
}
input = std::move(result_json_arr);
}
else {
throw std::invalid_argument("json_path is not valid for json. No element `" + next + "` found in json input. ");
}
}
}
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
rlib::opt_parser args(argc, argv); rlib::opt_parser args(argc, argv);
if(args.getBoolArg("-h", "--help")) { if(args.getBoolArg("-h", "--help")) {
...@@ -35,13 +60,10 @@ int main(int argc, char **argv) { ...@@ -35,13 +60,10 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
auto json_path = args.getSubCommand("").replace("\\", "/").strip("/"); auto json_path = args.getSubCommand("").replace("\\", "/").strip("/ \t");
json input; json input;
std::cin >> input; std::cin >> input;
for(auto &next : json_path.split('/')) { naive_json_access_path(input, json_path);
if(!next.empty())
input = input[next];
}
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment