Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
JSONPath

A strict RFC 9535 JSONPath implementation. More...

Classes

class  sourcemeta::core::JSONPath
class  sourcemeta::core::JSONPathParseError

Detailed Description

A strict RFC 9535 JSONPath implementation.

This functionality is included as follows:

#include <sourcemeta/core/jsonpath.h>

Class Documentation

◆ sourcemeta::core::JSONPath

class sourcemeta::core::JSONPath

A parsed RFC 9535 JSONPath query that can be evaluated many times.

Public Types

enum class  FilterFunctionName : std::uint8_t {
  Length , Count , Match , Search ,
  Value
}
 The function extensions that filter expressions can invoke. More...
enum class  FilterComparisonOperator : std::uint8_t {
  Equal , NotEqual , Less , LessEqual ,
  Greater , GreaterEqual
}
 The operators that filter comparisons can use. More...
enum class  SegmentKind : std::uint8_t { SingleName , SingleIndex , General }
 The precomputed shape of a query segment. More...
using Callback
 The callback invoked for every query result node.
using Selector
 A single selector within a query segment.

Public Member Functions

 JSONPath (const JSON::StringView expression)
auto evaluate (const JSON &document, const Callback &callback) const -> void
auto to_json () const -> JSON

Static Public Member Functions

static auto normalize (const WeakPointer &location) -> JSON::String
static auto from_json (const JSON &value) -> std::optional< JSONPath >

Member Typedef Documentation

◆ Callback

Initial value:
std::function<void(const JSON &value, const WeakPointer &location)>
Definition json_value.h:39
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:44

The callback invoked for every query result node.

◆ Selector

Initial value:
A selector that matches a single array element by position.
Definition jsonpath.h:61
A selector that matches a single object member by name.
Definition jsonpath.h:50
A selector that matches a range of array elements.
Definition jsonpath.h:67
A selector that matches every member or element of a node.
Definition jsonpath.h:58

A single selector within a query segment.

Member Enumeration Documentation

◆ FilterComparisonOperator

enum class sourcemeta::core::JSONPath::FilterComparisonOperator : std::uint8_t
strong

The operators that filter comparisons can use.

Enumerator
Equal 

Both sides are equal.

NotEqual 

Both sides are not equal.

Less 

The left side orders before the right side.

LessEqual 

The left side orders before or equals the right side.

Greater 

The right side orders before the left side.

GreaterEqual 

The right side orders before or equals the left side.

◆ FilterFunctionName

enum class sourcemeta::core::JSONPath::FilterFunctionName : std::uint8_t
strong

The function extensions that filter expressions can invoke.

Enumerator
Length 

The length of a string, array, or object value.

Count 

The number of nodes a query selects.

Match 

Whether a string entirely matches a regular expression.

Search 

Whether a string contains a match of a regular expression.

Value 

The value of the single node a query selects.

◆ SegmentKind

enum class sourcemeta::core::JSONPath::SegmentKind : std::uint8_t
strong

The precomputed shape of a query segment.

Enumerator
SingleName 

A single name selector applied to children.

SingleIndex 

A single index selector applied to children.

General 

Any other combination of selectors.

Constructor & Destructor Documentation

◆ JSONPath()

sourcemeta::core::JSONPath::JSONPath ( const JSON::StringView expression)
explicit

Parse a query expression, throwing on invalid input. For example:

#include <sourcemeta/core/jsonpath.h>
const sourcemeta::core::JSONPath path{"$.store.book[0].title"};
Definition jsonpath.h:43

Member Function Documentation

◆ evaluate()

auto sourcemeta::core::JSONPath::evaluate ( const JSON & document,
const Callback & callback ) const -> void

Evaluate the query against a document, invoking the callback once per result node. For example:

#include <sourcemeta/core/jsonpath.h>
#include <cassert>
const sourcemeta::core::JSON document{
sourcemeta::core::parse_json("{ \"foo\": [ 1, 2 ] }")};
const sourcemeta::core::JSONPath path{"$.foo[0]"};
path.evaluate(document, [](const auto &value, const auto &location) {
assert(value.is_integer());
assert(location.size() == 2);
});
SOURCEMETA_CORE_JSON_EXPORT auto parse_json(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON
auto evaluate(const JSON &document, const Callback &callback) const -> void

◆ from_json()

auto sourcemeta::core::JSONPath::from_json ( const JSON & value) -> std::optional< JSONPath >
staticnodiscard

Deserialize a query from its expression string as JSON, returning no result on invalid input. For example:

#include <sourcemeta/core/jsonpath.h>
#include <cassert>
const sourcemeta::core::JSON input{"$.foo[0]"};
const auto path{sourcemeta::core::JSONPath::from_json(input)};
assert(path.has_value());
static auto from_json(const JSON &value) -> std::optional< JSONPath >

◆ normalize()

auto sourcemeta::core::JSONPath::normalize ( const WeakPointer & location) -> JSON::String
staticnodiscard

Serialize a location into the RFC 9535 normalized path form. For example:

#include <sourcemeta/core/jsonpath.h>
#include <cassert>
const sourcemeta::core::JSON document{
sourcemeta::core::parse_json("{ \"foo\": [ 1, 2 ] }")};
const sourcemeta::core::JSONPath path{"$.foo[0]"};
path.evaluate(document, [](const auto &value, const auto &location) {
"$['foo'][0]");
});
static auto normalize(const WeakPointer &location) -> JSON::String

◆ to_json()

auto sourcemeta::core::JSONPath::to_json ( ) const -> JSON
nodiscard

Serialize the query into its expression string as JSON. For example:

#include <sourcemeta/core/jsonpath.h>
#include <cassert>
const sourcemeta::core::JSONPath path{"$.foo[0]"};
const sourcemeta::core::JSON result{path.to_json()};
assert(result.is_string());
assert(result.to_string() == "$.foo[0]");
SOURCEMETA_FORCEINLINE auto is_string() const noexcept -> bool
Definition json_value.h:546
SOURCEMETA_FORCEINLINE auto to_string() const noexcept -> const String &
Definition json_value.h:702
auto to_json() const -> JSON

◆ sourcemeta::core::JSONPathParseError

class sourcemeta::core::JSONPathParseError

An error that represents a JSONPath parsing failure

Inheritance diagram for sourcemeta::core::JSONPathParseError:

Public Member Functions

 JSONPathParseError (const std::uint64_t column)
 Construct an error given the column number where parsing failed.
auto column () const noexcept -> std::uint64_t
 Get the column number of the error.