Class JsonPathParser

java.lang.Object
dev.blaauwendraad.masker.json.path.JsonPathParser

public class JsonPathParser extends Object
Parses a jsonpath literal into a JsonPath object.

The following features from jsonpath specification are not supported:

  • Descendant segments
  • Child segments
  • Name selectors
  • Array slice selectors
  • Index selectors
  • Filter selectors
  • Function extensions
  • Escape characters

The parser makes a couple of additional restrictions:

  • Numbers as key names are disallowed
  • A set of input jsonpath literals must not be ambiguous
An example of ambiguous set of queries is $.*.b and $.a.b. In this case, we cannot match forward the segments.
  • Constructor Details

    • JsonPathParser

      public JsonPathParser()
  • Method Details

    • parse

      @Nonnull public JsonPath parse(String literal)
      Parses an input literal into a JsonPath object. Throws IllegalArgumentException when the input literal does not follow the jsonpath specification.
      Parameters:
      literal - a jsonpath literal to be parsed.
      Returns:
      JsonPath object parsed from the literal.
    • tryParse

      public JsonPath tryParse(String literal)
      Parses an input literal into a JsonPath object. Returns null when the input literal does not follow the jsonpath specification.
      Parameters:
      literal - a jsonpath literal to be parsed.
      Returns:
      a JsonPath object parsed from the literal.
    • checkAmbiguity

      public void checkAmbiguity(Set<JsonPath> jsonPaths)
      Validates if the input set of JSONPath queries is ambiguous. Throws java.lang.IllegalArgumentException#IllegalArgumentException if it is.

      The method does a lexical sort of input jsonpath queries, iterates over sorted values and checks if any local pair is ambiguous.

      Parameters:
      jsonPaths - input set of jsonpath queries