simdjson  3.11.0
Ridiculously Fast JSON
jsonpathutil.h
1 #ifndef SIMDJSON_JSONPATHUTIL_H
2 #define SIMDJSON_JSONPATHUTIL_H
3 
4 #include <string>
5 #include <string_view>
6 
7 namespace simdjson {
13 inline std::string json_path_to_pointer_conversion(std::string_view json_path) {
14  size_t i = 0;
15 
16  // if JSONPath starts with $, skip it
17  if (!json_path.empty() && json_path.front() == '$') {
18  i = 1;
19  }
20  if (json_path.empty() || (json_path[i] != '.' &&
21  json_path[i] != '[')) {
22  return "-1"; // This is just a sentinel value, the caller should check for this and return an error.
23  }
24 
25  std::string result;
26  // Reserve space to reduce allocations, adjusting for potential increases due
27  // to escaping.
28  result.reserve(json_path.size() * 2);
29 
30  while (i < json_path.length()) {
31  if (json_path[i] == '.') {
32  result += '/';
33  } else if (json_path[i] == '[') {
34  result += '/';
35  ++i; // Move past the '['
36  while (i < json_path.length() && json_path[i] != ']') {
37  if (json_path[i] == '~') {
38  result += "~0";
39  } else if (json_path[i] == '/') {
40  result += "~1";
41  } else {
42  result += json_path[i];
43  }
44  ++i;
45  }
46  if (i == json_path.length() || json_path[i] != ']') {
47  return "-1"; // Using sentinel value that will be handled as an error by the caller.
48  }
49  } else {
50  if (json_path[i] == '~') {
51  result += "~0";
52  } else if (json_path[i] == '/') {
53  result += "~1";
54  } else {
55  result += json_path[i];
56  }
57  }
58  ++i;
59  }
60 
61  return result;
62 }
63 } // namespace simdjson
64 #endif // SIMDJSON_JSONPATHUTIL_H
The top level simdjson namespace, containing everything the library provides.
Definition: base.h:8
std::string json_path_to_pointer_conversion(std::string_view json_path)
Converts JSONPath to JSON Pointer.
Definition: jsonpathutil.h:13