simdjson 4.0.7
Ridiculously Fast JSON
Loading...
Searching...
No Matches
jsonpathutil.h
1#ifndef SIMDJSON_JSONPATHUTIL_H
2#define SIMDJSON_JSONPATHUTIL_H
3
4#include <string>
5#include "simdjson/common_defs.h"
6
7#include <utility>
8
9namespace simdjson {
15inline std::string json_path_to_pointer_conversion(std::string_view json_path) {
16 size_t i = 0;
17 // if JSONPath starts with $, skip it
18 // json_path.starts_with('$') requires C++20.
19 if (!json_path.empty() && json_path.front() == '$') {
20 i = 1;
21 }
22 if (i >= json_path.size() || (json_path[i] != '.' &&
23 json_path[i] != '[')) {
24 return "-1"; // This is just a sentinel value, the caller should check for this and return an error.
25 }
26
27 std::string result;
28 // Reserve space to reduce allocations, adjusting for potential increases due
29 // to escaping.
30 result.reserve(json_path.size() * 2);
31
32 while (i < json_path.length()) {
33 if (json_path[i] == '.') {
34 result += '/';
35 } else if (json_path[i] == '[') {
36 result += '/';
37 ++i; // Move past the '['
38 while (i < json_path.length() && json_path[i] != ']') {
39 if (json_path[i] == '~') {
40 result += "~0";
41 } else if (json_path[i] == '/') {
42 result += "~1";
43 } else {
44 result += json_path[i];
45 }
46 ++i;
47 }
48 if (i == json_path.length() || json_path[i] != ']') {
49 return "-1"; // Using sentinel value that will be handled as an error by the caller.
50 }
51 } else {
52 if (json_path[i] == '~') {
53 result += "~0";
54 } else if (json_path[i] == '/') {
55 result += "~1";
56 } else {
57 result += json_path[i];
58 }
59 }
60 ++i;
61 }
62
63 return result;
64}
65
66inline std::pair<std::string_view, std::string_view> get_next_key_and_json_path(std::string_view& json_path) {
67 std::string_view key;
68
69 if (json_path.empty()) {
70 return {key, json_path};
71 }
72 size_t i = 0;
73
74 // if JSONPath starts with $, skip it
75 if (json_path.front() == '$') {
76 i = 1;
77 }
78
79
80 if (i < json_path.length() && json_path[i] == '.') {
81 i += 1;
82 size_t key_start = i;
83
84 while (i < json_path.length() && json_path[i] != '[' && json_path[i] != '.') {
85 ++i;
86 }
87
88 key = json_path.substr(key_start, i - key_start);
89 } else if ((i+1 < json_path.size()) && json_path[i] == '[' && (json_path[i+1] == '\'' || json_path[i+1] == '"')) {
90 i += 2;
91 size_t key_start = i;
92 while (i < json_path.length() && json_path[i] != '\'' && json_path[i] != '"') {
93 ++i;
94 }
95
96 key = json_path.substr(key_start, i - key_start);
97
98 i += 2;
99 } 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"]
100 key = "*";
101 i += 3;
102 }
103
104
105 return std::make_pair(key, json_path.substr(i));
106}
107
108} // namespace simdjson
109#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.