$ show json-path/JsonPath
Jayway JsonPath is a mature Java library that implements Stefan Goessner's JSONPath specification, providing an XPath-like query language for JSON documents. It enables reading, filtering, writing, and transforming JSON data through a fluent API. The core entry point is the `JsonPath` class, which offers static methods like `read()` and `parse()`, returning `DocumentContext` for chained operations such as `set()`, `delete()`, `add()`, `put()`, `renameKey()`, and `map()`. The library supports a rich filter system via `Filter`, `Criteria`, and `Predicate` interfaces, allowing both inline path filters (e.g., `[?(@.price < 10)]`) and programmatic construction. It also includes a function system for aggregation (`min`, `max`, `avg`, `sum`, `stddev`) and text operations (`length`, `concat`). Architecturally, the library follows a Service Provider Interface (SPI) pattern with `JsonProvider` and `MappingProvider` abstractions, enabling pluggable JSON backends. It ships with providers for Jackson 2, Jackson 3, Gson, Json-smart, JsonOrg, Jakarta JSON-P, Jettison, and Tapestry. The `Configuration` class is immutable and built via a builder, centralizing options like `REQUIRE_PROPERTIES` and `DEFAULT_PATH_LEAF_TO_NULL`. Path compilation is cached via a configurable `CacheProvider` (default LRU). The separate `json-path-assert` module provides Hamcrest matchers (`hasJsonPath`, `withJsonPath`, `isJson`) for testing. External dependencies are minimal for the core module (only `net.minidev:json-smart` for the default provider), with optional dependencies for other providers. The library is built with Gradle, targets Java 17, and has extensive JUnit 5 test coverage including regression tests for hundreds of reported issues. It is designed for integration into any Java application that needs to query or manipulate JSON data programmatically.
The LLM identified 4 files worth highlighting for reuse:
Provides static factory methods for Hamcrest matchers that assert JSONPath existence, absence, and JSON validity. This module is fully self-contained (depends only on Hamcrest and the core json-path library) and can be extracted into any project that uses Hamcrest for testing JSON structures. It demonstrates a clean matcher pattern with type-safe generics and clear error messages.
Implements a fluent builder for constructing filter criteria (e.g., `where("price").lt(10).and("category").is("fiction")`). It is isolated from internal path evaluation and can be reused as a standalone predicate-building DSL. The class uses method chaining, supports logical combinators (AND, OR), and handles type coercion for comparisons. This pattern is directly applicable to any domain requiring composable filtering logic.
Defines a minimal SPI for caching compiled JSONPath expressions. The interface has a single method `getCache()` returning a `Cache` instance, and the provider can be set globally. The default implementation (`LRUCache`) is a simple, thread-safe LRU cache. This pattern is reusable for any library that needs to cache expensive computations with pluggable backends, and the code is small enough to extract without dependencies.
Implements the `avg` function for numeric arrays. It extends `AbstractAggregation` and demonstrates a clean strategy pattern for adding new aggregation functions. The class is self-contained, handles empty collections gracefully, and can be extracted as a template for implementing custom JSONPath functions. The function registration via `PathFunctionFactory` makes it easy to add without modifying core code.