Class TinyPath

java.lang.Object
com.dcsuibian.tinypath.TinyPath

public final class TinyPath extends Object
A compiled TinyPath expression that can be evaluated against JSON data.

TinyPath is a minimal JSON path expression language designed for IoT scenarios. Expressions are compiled once and can be evaluated repeatedly against different inputs.

TinyPath path = TinyPath.compile("$[\"sensors\"][0][\"value\"]");
String value = path.evaluate(json, String.class);
  • Method Summary

    Modifier and Type
    Method
    Description
    static TinyPath
    compile(String expression)
    Compiles a TinyPath expression.
    <T> T
    evaluate(String json, Class<T> type)
    Evaluates this expression against the given JSON string and converts the result to the specified type.
    <T> T
    evaluate(tools.jackson.databind.JsonNode root, Class<T> type)
    Evaluates this expression against a pre-parsed JsonNode and converts the result to the specified type.
    Returns the original expression string.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Method Details

    • compile

      public static TinyPath compile(String expression)
      Compiles a TinyPath expression.
      Parameters:
      expression - the TinyPath expression string
      Returns:
      a compiled TinyPath instance
      Throws:
      TinyPathException - if the expression contains a syntax error
    • evaluate

      public <T> T evaluate(String json, Class<T> type)
      Evaluates this expression against the given JSON string and converts the result to the specified type.

      Returns null when the path does not resolve — for example, when a field is missing, an array index is out of bounds, or a filter has no match.

      If you need to evaluate multiple expressions against the same JSON data, prefer evaluate(JsonNode, Class) with a pre-parsed JsonNode to avoid parsing the JSON string repeatedly.

      Type Parameters:
      T - the expected result type
      Parameters:
      json - the JSON string to evaluate against
      type - the expected result type class (e.g. String.class, Integer.class)
      Returns:
      the value at the path, or null if the path does not resolve
      Throws:
      TinyPathException - if the JSON string is invalid
    • evaluate

      public <T> T evaluate(tools.jackson.databind.JsonNode root, Class<T> type)
      Evaluates this expression against a pre-parsed JsonNode and converts the result to the specified type.

      Use this overload when evaluating multiple expressions against the same JSON data, so the JSON string is parsed only once via Json.MAPPER.

      Returns null when the path does not resolve — for example, when a field is missing, an array index is out of bounds, or a filter has no match.

      Type Parameters:
      T - the expected result type
      Parameters:
      root - the pre-parsed JSON tree to evaluate against
      type - the expected result type class (e.g. String.class, Integer.class)
      Returns:
      the value at the path, or null if the path does not resolve
    • toString

      public String toString()
      Returns the original expression string.
      Overrides:
      toString in class Object