Crate rsonpath_syntax
source ·Expand description
Complete, fast, and fully spec-compliant JSONPath query parser.
The crate exposes the JsonPathQuery type and the parse
function that converts a query string into the AST representation. The parsing
complies with the proposed JSONPath RFC specification.
A JSONPath query is a sequence of segments, each containing one or more selectors. There are two types of segments:
- child (Segment::Child), and
- descendant (Segment::Descendant);
and five different types of selectors:
- name (Selector::Name),
- wildcard (Selector::Wildcard),
- index (Selector::Index),
- slice,
- and filter.
Descriptions of each segment and selector can be found in the documentation of the relevant type in this crate, while the formal grammar is described in the RFC.
State of the crate
This is an in-development version that supports only name, index, and wildcard selectors. However, these are fully supported, tested, and fuzzed. The planned roadmap is:
- support slices
- support filters (without functions)
- support functions (including type check)
- polish the API
- 1.0.0 stable release
Examples
To create a query from a query string:
let query_string = "$..phoneNumbers[*].number";
let query = rsonpath_syntax::parse(query_string)?;
// Query structure is a linear sequence of segments:
// Descendant '..phoneNumbers', child wildcard, child 'number'.
assert_eq!(query.segments().len(), 3);
assert_eq!(
  query.segments()[0],
  Segment::Descendant(
    Selectors::one(
      Selector::Name(
        JsonString::new("phoneNumbers")
))));
assert_eq!(
  query.segments()[1],
  Segment::Child(
    Selectors::one(
      Selector::Wildcard
)));
assert_eq!(
  query.segments()[2],
  Segment::Child(
    Selectors::one(
      Selector::Name(
        JsonString::new("number")
))));Crate features
There are two optional features:
- arbitrary, which enables a dependency on the- arbitrarycrate to provide- Arbitraryimplementations on query types; this is used e.g. for fuzzing.
- color, which enables a dependency on the- owo_colorscrate to provide colorful- Displayrepresentations of- ParseError; see- ParseError::colored.
Modules
- Utility for building aJsonPathQueryprogrammatically.
- Error types for the crate.
- JSON number types expressible in a JSONPath query.
- JSON string types expressible in a JSONPath query.
Structs
- JSONPath query structure represented by a sequence ofSegments.
- JSONPath query parser.
- Configurable builder for aParserinstance.
- Collection of one or moreSelectorinstances.
- Slice selector defining the start and end bounds, as well as the step value and direction.
- Helper API for programmatically constructingSliceinstances.
Enums
- Directional index into a JSON array.
- JSONPath query segment.
- EachSegmentdefines one or more selectors. A selector produces one or more children/descendants of the node it is applied to.
- Directional step offset within a JSON array.
Functions
- Parse a JSONPath query string using defaultParserconfiguration.
Type Aliases
- Convenience alias forResultvalues returned by this crate.