DEV Community

noear
noear

Posted on

Snack4-JsonPath v4.0.36 Released (IETF RFC 9535 Standard Support)

Provides JSON DOM construction, encoding/decoding (serialization), retrieval, JsonPath queries, JsonSchema validation, and more.

<dependency>
  <groupId>org.noear</groupId>
  <artifactId>snack4-jsonpath</artifactId>
  <version>4.0.36</version>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Snack-JsonPath draws inspiration from JavaScript's var declaration for all variables and XML DOM's "everything is a Node" design philosophy. All underlying data is represented as ONode, which stands for "One node" - capable of representing any type and converting to any type.

  • Emphasizes document tree construction and manipulation capabilities
  • High-performance JSON Path queries (significantly faster than jayway.jsonpath). Compatible with both jayway.jsonpath and the IETF JSONPath (RFC 9535) standard (switchable via options). Delivering a robust JsonPath experience for the next decade.
  • Supports Json schema validation
  • Supports partial json5 features (unquoted keys, comments, etc.)
  • Prioritizes no-argument constructors + field encoding/decoding (reduces risks of injection-triggered actions)
  • Supports Java 8 to Java 25

Dependency packages:

Package Description
org.noear:snack4 Provides json dom construction and encoding/decoding support
org.noear:snack4-jsonpath Provides json path query support
org.noear:snack4-jsonschema Provides json schema validation support

Open source repository:

Documentation:

1. Recent Updates

  • Added snack4 Json Feature.Read_TrimString feature
  • Added snack4 JsonReader.iterableNext method (supports JSON streaming, particularly adapted for LLM stream output)
  • Added snack4 JsonReader.readLast method
  • Added snack4 ONode field type deserialization support
  • Added snack4 custom annotation retrieval support
  • Added snack4 Options.then method for chained building
  • Added snack4 CodecLib.patternCreators, patternDecoders, patternEncoders deduplication handling
  • Added snack4 MapperLib.schemaPatternMappers, typePatternMappers deduplication handling
  • Added snack4 AtomicBoolean, AtomicLong, AtomicInteger support
  • Added snack4 Optional built-in codec support (also customizable)
  • Added snack4 ONode:delete method to assist with jsonpath deletion
  • Added snack4-jsonschema JsonSchema default value generation
  • Added snack4-jsonschema type mapping mechanism, supporting wrapper or delivery types like Future, Optional
  • Optimized snack4 Iterable support (replacing previous Collection)
  • Optimized snack4 deserialization to automatically remove '@type' property declaration
  • Optimized snack4-jsonpath logical expression compatibility to support space-less "a=='a'"
  • Optimized snack4-jsonschema Optional type handling
  • Changed snack4 JsonReader.streamRead to readNext (former marked as deprecated)
  • Changed snack4-jsonpath JsonPath.delete to return bool (previously void)
  • Fixed snack4 ONode.ofBean and ofJson throwing exception when passed null
  • Fixed snack4 Json BeanDecoder not restoring array.item when null (incorrectly filtered)
  • Fixed snack4-jsonschema JsonSchema TypeRule.getSchemaTypeName not recognizing initial node as null (was recognized as undefined)
  • Fixed snack4-jsonpath JsonPathProvider:delete(root, path) out-of-bounds issue when deleting multiple array indexes

2. JSONPath Syntax Reference

Syntax Element Description
$ Root node identifier
@ Current node identifier (valid only in filter selectors)
[<selectors>] Child segment: selects zero or more child nodes of a node
.name Shorthand for ['name']
.* Shorthand for [*]
..[<selectors>] Descendant segment: selects zero or more descendants of a node
..name Shorthand for ..['name']
..* Shorthand for ..[*]
'name' Name selector: selects named child of an object
* Wildcard selector: selects all children of a node
3 Index selector: selects array index item (0-based)
0:100:5 Array slice selector: start:end:step for arrays
?<logical-expr> Filter selector: selects specific items using logical expressions
fun(@.foo) Filter function: calls function in filter expression (IETF standard)
.fun() Aggregate function: used as a segment (jayway style)

Filter selector syntax reference:

Syntax Description Precedence
(...) Grouping 5
name(...) Function extension 5
! Logical NOT 4
==,!=,<,<=,>,>= Relational comparison operators 3
&& Logical AND 2
`\ \ `

IETF JSONPath (RFC 9535) Standard Defined Operators (Supported)

Operator Description Example
== Left equals right (note: 1 does not equal '1') $[?(@.a == 1)]
!= Left does not equal right $[?(@.a != 1)]
< Left is less than right $[?(@.a < 1)]
<= Left is less than or equal to right $[?(@.a <= 1)]
> Left is greater than right $[?(@.a > 1)]
>= Left is greater than or equal to right $[?(@.a >= 1)]

jayway.jsonpath Extended Operators (Supported)

Operator Description Example
=~ Left matches regex [?(@.s =~ /foo.*?/i)]
in Left exists in right [?(@.s in ['S', 'M'])]
nin Left does not exist in right
subsetof Left is a subset of right [?(@.s subsetof ['S', 'M', 'L'])]
anyof Left has at least one intersection with right [?(@.s anyof ['M', 'L'])]
noneof Left has no intersection with right [?(@.s noneof ['M', 'L'])]
size Left (array or string) size should match right $[?(@.s size @.expected_size)]
empty Left (array or string) should be empty $[?(@.s empty false)]

IETF JSONPath (RFC 9535) Standard Defined Functions (Supported)

Function Description Parameter Type Result Type
length(x) Length of string, array, or object Value Number
count(x) Size of node list Node List Number
match(x,y) Regex full match Value, Value Boolean
search(x,y) Regex substring match Value, Value Boolean
value(x) Value of single node in node list Node List Value

jayway.jsonpath Functions (Supported)

Function Description Output Type
length() Length of string, array, or object Integer
min() Find minimum value in current numeric array Double
max() Find maximum value in current numeric array Double
avg() Calculate average of current numeric array Double
stddev() Calculate standard deviation of current numeric array Double
sum() Calculate sum of current numeric array Double
keys() Get property key set of current object Set<E>
concat(X) Concatenate an item or collection with current array into new array like input
append(X) Append an item or collection to current path's output array like input
first() Return first element of current array Depends on array element type
last() Return last element of current array Depends on array element type
index(X) Return element at index X in current array. X can be negative (counting from end) Depends on array element type

Top comments (0)