Record Class JsonPathQuery

java.lang.Object
java.lang.Record
ca.marcusdunn.jsonlens.path.core.query.JsonPathQuery
Record Components:
segments - the segments, in order

public record JsonPathQuery(List<Segment> segments) extends Record

A JSONPath query: the root identifier $ followed by zero or more segments (RFC 9535, Section 2.1.1).

Each segment operates on the nodelist of the previous segment. The first segment operates on the root node. The parser makes a query from text, and keeps shorthand forms as their bracket equivalents:

Query text Segments
$ none
$.store [Child[Name[store]]]
$.store.* [Child[Name[store]], Child[Wildcard[]]]
$..book[0, 'x'] [Descendant[Name[book]], Child[Index[0], Name[x]]]
JsonPathQuery query = JsonPathParser.standard().parse("$..book[0, 'x']").orElse(null);
Segment segment = query.segments().getFirst();
// segment: Descendant[selectors=[Name[name=book]]]
List<Selector> selectors = query.segments().get(1).selectors();
// selectors: [Index[index=0], Name[name=x]]
  • Constructor Details

    • JsonPathQuery

      public JsonPathQuery(List<Segment> segments)
      Makes a query.
      Parameters:
      segments - the segments, in order
  • Method Details

    • toString

      public String toString()

      Writes the query as query text in bracket notation.

      Names and strings are in single quotes, with all necessary escapes. Parentheses occur only where the precedence of the operators makes them necessary. The parser makes an equal query from this text:

      // $.store.book[?length(@.title) > 10]['title'], made without the parser.
      LogicalExpression longTitle = new LogicalExpression.Comparison(
              new FunctionCall.Value("length", List.of(new FunctionArgument.Value(
                      new SingularQuery(Identifier.CURRENT, List.of(new SingularSegment.Name("title")))))),
              ComparisonOperator.GREATER,
              new Literal.NumberLiteral(BigDecimal.TEN));
      JsonPathQuery query = new JsonPathQuery(List.of(
              new Segment.Child(List.of(new Selector.Name("store"))),
              new Segment.Child(List.of(new Selector.Name("book"))),
              new Segment.Child(List.of(new Selector.Filter(longTitle))),
              new Segment.Child(List.of(new Selector.Name("title")))));
      
      String text = query.toString(); // $['store']['book'][?length(@['title']) > 10]['title']
      
      Specified by:
      toString in class Record
      Returns:
      the query text
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • segments

      public List<Segment> segments()
      Returns the value of the segments record component.
      Returns:
      the value of the segments record component