#ifndef SIMDJSON_JSONPATHUTIL_H #define SIMDJSON_JSONPATHUTIL_H #include #include 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 if (!json_path.empty() && json_path.front() == '$') { i = 1; } if (json_path.empty() || (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; } } // namespace simdjson #endif // SIMDJSON_JSONPATHUTIL_H