mirror of
https://github.com/simdjson/simdjson
synced 2026-06-08 17:27:07 +00:00
d84c934768
Doing that will emit warning at https://github.com/microsoft/STL/blob/vs-2022-17.13/stl/inc/string_view#L12.
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
#ifndef SIMDJSON_JSONPATHUTIL_H
|
|
#define SIMDJSON_JSONPATHUTIL_H
|
|
|
|
#include <string>
|
|
#include "simdjson/common_defs.h"
|
|
|
|
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
|