mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
fb9a689dff
* wip brute-force wildcard for json_path * wip - bruteforce surface wildcard with result * partially handle keys with wildcard * wip - nested paths/pointers on wildcard results * wip * wip - handling child properties of wildcard result * done - handling child properties of wildcard result * fix key * add support for wildcard for arrays * handle array INCORRECT_TYPE * rename at_path_new to at_path_with_wildcard * add benchmark * fix benchmark * use memcmp * minor improvements * refactor to tail recursion * nit * approximately 30% improvement in runtime * nit * corrected logic * nit * cleanup * add some initial tests * cleanup 2 * modified: CMakeLists.txt * cleanup * cleanup * cleanup * cleanup * restore examples/quickstart/CMakeLists.txt * cleanup * restore quickstart.cpp * revert array-inl.h * cleanup array-inl.h * revert object-inl.h * cleanup object-inl.h * cleanup * nit * nit * add test * address some feedbacks * address additional feedbacks (copilot) * final changes based on feedback - reduce string allocations * fix bug in object-inl.h * fix logic for wildcards inside arrays * add test for wildcard in nested array * refactor and create util for getting key and json path * minor error handling * refactor process_json_path_of_child_element from recursion to loop in order to prevent stack overflow * fix bug with array, add boundary check to get_next_key_and_json_path function * add more tests * fix boundary check and unnecessary string allocation * minor changes * fix * fix jsonpathutil * remove unneccessary string allocation * some minor fixes * documentation * removing printout * various fixes * reorg of the CI test file --------- Co-authored-by: Daniel Lemire <daniel@lemire.me>
110 lines
2.9 KiB
C++
110 lines
2.9 KiB
C++
#ifndef SIMDJSON_JSONPATHUTIL_H
|
|
#define SIMDJSON_JSONPATHUTIL_H
|
|
|
|
#include <string>
|
|
#include "simdjson/common_defs.h"
|
|
|
|
#include <utility>
|
|
|
|
namespace simdjson {
|
|
/**
|
|
* Converts JSONPath to JSON Pointer.
|
|
* @param json_path The JSONPath string to be converted.
|
|
* @return A string containing the equivalent JSON Pointer.
|
|
*/
|
|
inline std::string json_path_to_pointer_conversion(std::string_view json_path) {
|
|
size_t i = 0;
|
|
// if JSONPath starts with $, skip it
|
|
// json_path.starts_with('$') requires C++20.
|
|
if (!json_path.empty() && json_path.front() == '$') {
|
|
i = 1;
|
|
}
|
|
if (i >= json_path.size() || (json_path[i] != '.' &&
|
|
json_path[i] != '[')) {
|
|
return "-1"; // This is just a sentinel value, the caller should check for this and return an error.
|
|
}
|
|
|
|
std::string result;
|
|
// Reserve space to reduce allocations, adjusting for potential increases due
|
|
// to escaping.
|
|
result.reserve(json_path.size() * 2);
|
|
|
|
while (i < json_path.length()) {
|
|
if (json_path[i] == '.') {
|
|
result += '/';
|
|
} else if (json_path[i] == '[') {
|
|
result += '/';
|
|
++i; // Move past the '['
|
|
while (i < json_path.length() && json_path[i] != ']') {
|
|
if (json_path[i] == '~') {
|
|
result += "~0";
|
|
} else if (json_path[i] == '/') {
|
|
result += "~1";
|
|
} else {
|
|
result += json_path[i];
|
|
}
|
|
++i;
|
|
}
|
|
if (i == json_path.length() || json_path[i] != ']') {
|
|
return "-1"; // Using sentinel value that will be handled as an error by the caller.
|
|
}
|
|
} else {
|
|
if (json_path[i] == '~') {
|
|
result += "~0";
|
|
} else if (json_path[i] == '/') {
|
|
result += "~1";
|
|
} else {
|
|
result += json_path[i];
|
|
}
|
|
}
|
|
++i;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
inline std::pair<std::string_view, std::string_view> get_next_key_and_json_path(std::string_view& json_path) {
|
|
std::string_view key;
|
|
|
|
if (json_path.empty()) {
|
|
return {key, json_path};
|
|
}
|
|
size_t i = 0;
|
|
|
|
// if JSONPath starts with $, skip it
|
|
if (json_path.front() == '$') {
|
|
i = 1;
|
|
}
|
|
|
|
|
|
if (i < json_path.length() && json_path[i] == '.') {
|
|
i += 1;
|
|
size_t key_start = i;
|
|
|
|
while (i < json_path.length() && json_path[i] != '[' && json_path[i] != '.') {
|
|
++i;
|
|
}
|
|
|
|
key = json_path.substr(key_start, i - key_start);
|
|
} else if ((i+1 < json_path.size()) && json_path[i] == '[' && (json_path[i+1] == '\'' || json_path[i+1] == '"')) {
|
|
i += 2;
|
|
size_t key_start = i;
|
|
while (i < json_path.length() && json_path[i] != '\'' && json_path[i] != '"') {
|
|
++i;
|
|
}
|
|
|
|
key = json_path.substr(key_start, i - key_start);
|
|
|
|
i += 2;
|
|
} else if ((i+2 < json_path.size()) && json_path[i] == '[' && json_path[i+1] == '*' && json_path[i+2] == ']') { // i.e [*].additional_keys or [*]["additional_keys"]
|
|
key = "*";
|
|
i += 3;
|
|
}
|
|
|
|
|
|
return std::make_pair(key, json_path.substr(i));
|
|
}
|
|
|
|
} // namespace simdjson
|
|
#endif // SIMDJSON_JSONPATHUTIL_H
|