Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonpath.h
1#ifndef SOURCEMETA_CORE_JSONPATH_H_
2#define SOURCEMETA_CORE_JSONPATH_H_
3
4#ifndef SOURCEMETA_CORE_JSONPATH_EXPORT
5#include <sourcemeta/core/jsonpath_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/jsonpath_error.h>
10// NOLINTEND(misc-include-cleaner)
11
12#include <sourcemeta/core/json.h>
13#include <sourcemeta/core/jsonpointer.h>
14#include <sourcemeta/core/regex.h>
15
16#include <cstdint> // std::int64_t, std::uint8_t
17#include <functional> // std::function
18#include <optional> // std::optional
19#include <variant> // std::variant
20#include <vector> // std::vector
21
30
31namespace sourcemeta::core {
32
33// Exporting symbols that depends on the standard C++ library is considered
34// safe.
35// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
36#if defined(_MSC_VER)
37#pragma warning(push)
38#pragma warning(disable : 4251)
39#endif
40
43class SOURCEMETA_CORE_JSONPATH_EXPORT JSONPath {
44public:
46 using Callback =
47 std::function<void(const JSON &value, const WeakPointer &location)>;
48
50 struct SelectorName {
54 JSON::Object::hash_type hash;
55 };
56
59
63 std::int64_t index;
64 };
65
69 std::optional<std::int64_t> start;
71 std::optional<std::int64_t> end;
73 std::int64_t step;
74 };
75
77 enum class FilterFunctionName : std::uint8_t {
79 Length,
81 Count,
85 Search,
87 Value
88 };
89
91 enum class FilterComparisonOperator : std::uint8_t {
93 Equal,
95 NotEqual,
97 Less,
99 LessEqual,
101 Greater,
103 GreaterEqual
104 };
105
106#if !defined(DOXYGEN)
107 // Required by the recursive grammar and defined further below
108 struct Segment;
109#endif
110
112 struct FilterQuery {
116 std::vector<Segment> segments;
119 };
120
121#if !defined(DOXYGEN)
122 // Required by the recursive grammar and defined further below
123 struct FilterOperand;
124#endif
125
131 std::vector<FilterOperand> arguments;
134 std::optional<Regex> compiled;
135 };
136
140 std::variant<JSON, FilterQuery, FilterFunctionCall> value;
141 };
142
152
154 struct FilterTest {
158 std::variant<FilterQuery, FilterFunctionCall> subject;
159 };
160
161#if !defined(DOXYGEN)
162 // Required by the recursive grammar and defined further below
163 struct FilterExpression;
164#endif
165
169 std::vector<FilterExpression> children;
170 };
171
175 std::vector<FilterExpression> children;
176 };
177
181 std::vector<FilterExpression> children;
182 };
183
184#if !defined(DOXYGEN)
185 // For fast internal dispatching. It must stay in sync with the expression
186 // variant below
187 enum class FilterExpressionKind : std::uint8_t {
188 Comparison = 0,
189 Test,
190 Conjunction,
191 Disjunction,
192 Negation
193 };
194#endif
195
203
210
214
215#if !defined(DOXYGEN)
216 // For fast internal dispatching. It must stay in sync with the variant above
217 enum class SelectorKind : std::uint8_t {
218 Name = 0,
219 Wildcard,
220 Index,
221 Slice,
222 Filter
223 };
224#endif
225
227 enum class SegmentKind : std::uint8_t {
229 SingleName,
231 SingleIndex,
233 General
234 };
235
237 struct Segment {
243 std::vector<Selector> selectors;
244 };
245
247 struct Query {
249 std::vector<Segment> segments;
250 };
251
259 explicit JSONPath(const JSON::StringView expression);
260
276 auto evaluate(const JSON &document, const Callback &callback) const -> void;
277
293 [[nodiscard]] static auto normalize(const WeakPointer &location)
294 -> JSON::String;
295
307 [[nodiscard]] auto to_json() const -> JSON;
308
320 [[nodiscard]] static auto from_json(const JSON &value)
321 -> std::optional<JSONPath>;
322
323private:
324 JSON::String expression_;
325 Query query_;
326};
327
328#if defined(_MSC_VER)
329#pragma warning(pop)
330#endif
331
332} // namespace sourcemeta::core
333
334#endif
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
std::basic_string_view< Char, CharTraits > StringView
The string view type used by the JSON document.
Definition json_value.h:54
Definition json_value.h:39
@ Index
A map of index labels carrying no RDF ranging over an object.
Definition jsonld_materialize.h:116
static auto from_json(const JSON &value) -> std::optional< JSONPath >
static auto normalize(const WeakPointer &location) -> JSON::String
SegmentKind
The precomputed shape of a query segment.
Definition jsonpath.h:227
FilterFunctionName
The function extensions that filter expressions can invoke.
Definition jsonpath.h:77
std::function< void(const JSON &value, const WeakPointer &location)> Callback
The callback invoked for every query result node.
Definition jsonpath.h:46
auto evaluate(const JSON &document, const Callback &callback) const -> void
JSONPath(const JSON::StringView expression)
auto to_json() const -> JSON
FilterComparisonOperator
The operators that filter comparisons can use.
Definition jsonpath.h:91
std::variant< SelectorName, SelectorWildcard, SelectorIndex, SelectorSlice, SelectorFilter > Selector
A single selector within a query segment.
Definition jsonpath.h:212
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:44
@ Match
The verifier corresponds to the challenge.
Definition oauth_pkce.h:36
A comparison between two filter operands.
Definition jsonpath.h:144
FilterOperand right
The right side of the comparison.
Definition jsonpath.h:150
FilterComparisonOperator operation
The operator to compare with.
Definition jsonpath.h:148
FilterOperand left
The left side of the comparison.
Definition jsonpath.h:146
A conjunction of filter expressions.
Definition jsonpath.h:167
std::vector< FilterExpression > children
The expressions that must all hold.
Definition jsonpath.h:169
A disjunction of filter expressions.
Definition jsonpath.h:173
std::vector< FilterExpression > children
The expressions of which at least one must hold.
Definition jsonpath.h:175
A logical expression within a filter selector.
Definition jsonpath.h:197
std::variant< FilterComparison, FilterTest, FilterConjunction, FilterDisjunction, FilterNegation > value
The comparison, test, or combination this expression stands for.
Definition jsonpath.h:201
A function invocation within a filter expression.
Definition jsonpath.h:127
std::optional< Regex > compiled
Definition jsonpath.h:134
FilterFunctionName function
The function to invoke.
Definition jsonpath.h:129
std::vector< FilterOperand > arguments
The arguments to invoke the function with.
Definition jsonpath.h:131
A negated parenthesized filter expression.
Definition jsonpath.h:179
std::vector< FilterExpression > children
The single expression whose outcome is negated.
Definition jsonpath.h:181
A comparison side or function argument within a filter expression.
Definition jsonpath.h:138
std::variant< JSON, FilterQuery, FilterFunctionCall > value
The literal, query, or function invocation this operand stands for.
Definition jsonpath.h:140
A query embedded in a filter expression.
Definition jsonpath.h:112
bool singular
Whether the query selects at most one node.
Definition jsonpath.h:118
bool relative
Whether the query starts at the candidate node instead of the root.
Definition jsonpath.h:114
std::vector< Segment > segments
The segments of the query.
Definition jsonpath.h:116
An existence or function test within a filter expression.
Definition jsonpath.h:154
bool negated
Whether the outcome of the test is negated.
Definition jsonpath.h:156
std::variant< FilterQuery, FilterFunctionCall > subject
The query or function invocation under test.
Definition jsonpath.h:158
The compiled representation of a whole query.
Definition jsonpath.h:247
std::vector< Segment > segments
The segments of the query.
Definition jsonpath.h:249
A step in a query.
Definition jsonpath.h:237
SegmentKind kind
The precomputed shape of the segment for fast dispatching.
Definition jsonpath.h:241
std::vector< Selector > selectors
The selectors the segment applies.
Definition jsonpath.h:243
bool descendant
Whether the segment also applies to every descendant of its input.
Definition jsonpath.h:239
FilterExpression expression
The logical expression to apply.
Definition jsonpath.h:208
A selector that matches a single array element by position.
Definition jsonpath.h:61
std::int64_t index
The element position, where a negative value counts from the end.
Definition jsonpath.h:63
A selector that matches a single object member by name.
Definition jsonpath.h:50
JSON::Object::hash_type hash
The precomputed object key hash of the member name.
Definition jsonpath.h:54
JSON::String name
The decoded member name.
Definition jsonpath.h:52
A selector that matches a range of array elements.
Definition jsonpath.h:67
std::int64_t step
The distance between selected positions.
Definition jsonpath.h:73
std::optional< std::int64_t > start
The position the range starts at, if any.
Definition jsonpath.h:69
A selector that matches every member or element of a node.
Definition jsonpath.h:58