Class JsonPathUtils
java.lang.Object
de.xima.fc.utils.JsonPathUtils
Helper class for JSON paths. Follows RFC 9335 - JSONPath: Query
Expressions for JSON
- Since:
- 8.0.0
- Author:
- XIMA Media GmbH
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumThe quoting style for JSON path string literals. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringReturns the any-path-segment, i.e., "*", as per RFC 9535 - Selectors.static StringEncodes a string for a JSON path string literal with double quotes, as per RFC 9535 - name selector.static StringEncodes a string for a JSON path string literal with single quotes, as per RFC 9535 - name selector.static booleanChecks whether the given JSON member name is a shorthand member name as per RFC 9535 - syntax.static booleanisRootPath(String jsonPath) Checks whether the given JSON path is the root path, i.e., "$".static StringjoinPathSegment(Object[] segments, int start, int end) static StringjoinPathSegment(Object[] segments, int start, int end, JsonPathUtils.QuoteStyle quoteStyle) static StringjsonPathIndexSegment(int index) Constructs a JSON path index segment for a given array index, as per RFC 9535 - name syntax.static StringjsonPathNameSegment(String name) Constructs a JSON path name segment for a given object property name, as per RFC 9535 - name syntax, using the default quote style (single quotes).static StringjsonPathNameSegment(String name, JsonPathUtils.QuoteStyle quoteStyle) Constructs a JSON path name segment for a given object property name, as per RFC 9535 - name syntax, using the given quote style.static StringjsonPathSegment(Object segment) Returns eitherjsonPathIndexSegment(int)orjsonPathNameSegment(String)depending on the type of the given segment, using the default quote style (single quotes).static StringjsonPathSegment(Object segment, JsonPathUtils.QuoteStyle quoteStyle) Returns eitherjsonPathIndexSegment(int)orjsonPathNameSegment(String)depending on the type of the given segment, using the given quote style.static StringjsonSubPath(String path, Object key) Constructs a JSON path for a sub-element at the given name or index of the given path, using the default quote style (single quotes).static StringjsonSubPath(String path, Object key, JsonPathUtils.QuoteStyle quoteStyle) Constructs a JSON path for a sub-element at the given name or index of the given path, using the given quote style.static StringjsonSubPathAtIndex(String parentPath, int index) Constructs a JSON path for a sub-element at the given array index of the given parent path.static StringjsonSubPathAtName(String parentPath, String name) Constructs a JSON path for a sub-element at the given object property name of the given parent path, using the default quote style (single quotes).static StringjsonSubPathAtName(String parentPath, String name, JsonPathUtils.QuoteStyle quoteStyle) Constructs a JSON path for a sub-element at the given object property name of the given parent path, using the given quote style.static ObjectreadValueAtJsonPath(Map<String, Object> json, Object pathValue) Returns the value of the object defined by the given JSON path if it exists.static StringReturns the root path segment, i.e., "$".
-
Method Details
-
anyPathSegment
Returns the any-path-segment, i.e., "*", as per RFC 9535 - Selectors.- Returns:
- The any-path-segment.
-
encodeForJsonPathDoubleQuotedStringLiteral
Encodes a string for a JSON path string literal with double quotes, as per RFC 9535 - name selector. Assumes the string will be enclosed in double quotes.name-selector = string-literal string-literal = %x22 *double-quoted %x22 / ; "string" %x27 *single-quoted %x27 ; 'string' double-quoted = unescaped / %x27 / ; ' ESC %x22 / ; \" ESC escapable single-quoted = unescaped / %x22 / ; " ESC %x27 / ; \' ESC escapable ESC = %x5C ; \ backslash unescaped = %x20-21 / ; see RFC 8259 ; omit 0x22 " %x23-26 / ; omit 0x27 ' %x28-5B / ; omit 0x5C \ %x5D-D7FF / ; skip surrogate code points %xE000-10FFFF escapable = %x62 / ; b BS backspace U+0008 %x66 / ; f FF form feed U+000C %x6E / ; n LF line feed U+000A %x72 / ; r CR carriage return U+000D %x74 / ; t HT horizontal tab U+0009 "/" / ; / slash (solidus) U+002F "\" / ; \ backslash (reverse solidus) U+005C (%x75 hexchar) ; uXXXX U+XXXX hexchar = non-surrogate / (high-surrogate "\" %x75 low-surrogate) non-surrogate = ((DIGIT / "A"/"B"/"C" / "E"/"F") 3HEXDIG) / ("D" %x30-37 2HEXDIG ) high-surrogate = "D" ("8"/"9"/"A"/"B") 2HEXDIG low-surrogate = "D" ("C"/"D"/"E"/"F") 2HEXDIG HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"Notes:- Double-quoted strings follow the JSON string syntax (Section 7 of [RFC8259]); single-quoted strings follow an analogous pattern. No attempt was made to improve on this syntax, so if it is desired to escape characters with scalar values above 0xFFFF, such as U+1F041 ("🁁", DOMINO TILE HORIZONTAL-02-02), they need to be represented by a pair of surrogate escapes ("🁁" in this case).
- Alphabetic characters in quoted strings are case-insensitive in ABNF, so each of the hexadecimal digits within \u escapes (as specified in rules referenced by hexchar) can be either lowercase or uppercase, while the u in \u needs to be lowercase (indicated as %x75).
- Parameters:
name- The JSON path name to encode.- Returns:
- The encoded JSON path name.
-
encodeForJsonPathSingleQuotedStringLiteral
Encodes a string for a JSON path string literal with single quotes, as per RFC 9535 - name selector. Assumes the string will be enclosed in single quotes.name-selector = string-literal string-literal = %x22 *double-quoted %x22 / ; "string" %x27 *single-quoted %x27 ; 'string' double-quoted = unescaped / %x27 / ; ' ESC %x22 / ; \" ESC escapable single-quoted = unescaped / %x22 / ; " ESC %x27 / ; \' ESC escapable ESC = %x5C ; \ backslash unescaped = %x20-21 / ; see RFC 8259 ; omit 0x22 " %x23-26 / ; omit 0x27 ' %x28-5B / ; omit 0x5C \ %x5D-D7FF / ; skip surrogate code points %xE000-10FFFF escapable = %x62 / ; b BS backspace U+0008 %x66 / ; f FF form feed U+000C %x6E / ; n LF line feed U+000A %x72 / ; r CR carriage return U+000D %x74 / ; t HT horizontal tab U+0009 "/" / ; / slash (solidus) U+002F "\" / ; \ backslash (reverse solidus) U+005C (%x75 hexchar) ; uXXXX U+XXXX hexchar = non-surrogate / (high-surrogate "\" %x75 low-surrogate) non-surrogate = ((DIGIT / "A"/"B"/"C" / "E"/"F") 3HEXDIG) / ("D" %x30-37 2HEXDIG ) high-surrogate = "D" ("8"/"9"/"A"/"B") 2HEXDIG low-surrogate = "D" ("C"/"D"/"E"/"F") 2HEXDIG HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"Notes:- Double-quoted strings follow the JSON string syntax (Section 7 of [RFC8259]); single-quoted strings follow an analogous pattern. No attempt was made to improve on this syntax, so if it is desired to escape characters with scalar values above 0xFFFF, such as U+1F041 ("🁁", DOMINO TILE HORIZONTAL-02-02), they need to be represented by a pair of surrogate escapes ("🁁" in this case).
- Alphabetic characters in quoted strings are case-insensitive in ABNF, so each of the hexadecimal digits within \u escapes (as specified in rules referenced by hexchar) can be either lowercase or uppercase, while the u in \u needs to be lowercase (indicated as %x75).
- Parameters:
name- The JSON path name to encode.- Returns:
- The encoded JSON path name.
-
isJsonPathShorthandMemberName
Checks whether the given JSON member name is a shorthand member name as per RFC 9535 - syntax.member-name-shorthand = name-first *name-char name-first = ALPHA / "_" / %x80-D7FF / ; skip surrogate code points %xE000-10FFFF name-char = name-first / DIGIT DIGIT = %x30-39 ; 0-9 ALPHA = %x41-5A / %x61-7A ; A-Z / a-z- Parameters:
name- The JSON path name to check.- Returns:
- true if the given JSON member name needs is a shorthand member name, false otherwise.
-
isRootPath
Checks whether the given JSON path is the root path, i.e., "$".- Parameters:
jsonPath- The JSON path to check.- Returns:
- true if the given JSON path is the root path, false otherwise.
-
joinPathSegment
-
joinPathSegment
public static String joinPathSegment(Object[] segments, int start, int end, JsonPathUtils.QuoteStyle quoteStyle) -
jsonPathIndexSegment
Constructs a JSON path index segment for a given array index, as per RFC 9535 - name syntax. For example, given the index2, this method returns[2].- Parameters:
index- An array index.- Returns:
- The index segment for the given array index.
- Throws:
IllegalArgumentException- If the given index is negative.
-
jsonPathNameSegment
Constructs a JSON path name segment for a given object property name, as per RFC 9535 - name syntax, using the default quote style (single quotes). For example, given the namefoo, this method returns.foo, and for the namefoo bar, it returns['foo bar'].- Parameters:
name- A property name.- Returns:
- The name segment for the given property name.
-
jsonPathNameSegment
Constructs a JSON path name segment for a given object property name, as per RFC 9535 - name syntax, using the given quote style. For example, given the namefoo, this method returns.foo, and for the namefoo bar, it returns["foo bar"].- Parameters:
name- A property name.quoteStyle- The quoting style for the JSON path string literal.- Returns:
- The name segment for the given property name.
-
jsonPathSegment
Returns eitherjsonPathIndexSegment(int)orjsonPathNameSegment(String)depending on the type of the given segment, using the default quote style (single quotes). For a number, the index segment is returned, and for any other object, the name segment is returned.- Parameters:
segment- The segment, either a Number (for array indices) or any other object (for property names).- Returns:
- The JSON path segment for the given segment.
-
jsonPathSegment
Returns eitherjsonPathIndexSegment(int)orjsonPathNameSegment(String)depending on the type of the given segment, using the given quote style. For a number, the index segment is returned, and for any other object, the name segment is returned.- Parameters:
segment- The segment, either a Number (for array indices) or any other object (for property names).quoteStyle- The quoting style for the JSON path string literal.- Returns:
- The JSON path segment for the given segment.
-
jsonSubPath
Constructs a JSON path for a sub-element at the given name or index of the given path, using the default quote style (single quotes). The given key must be either a String (for object property names) or an Integer (for array indices).- Parameters:
path- The parent JSON path.key- The name or index of the sub-element.- Returns:
- The JSON path for the sub-element.
-
jsonSubPath
Constructs a JSON path for a sub-element at the given name or index of the given path, using the given quote style. The given key must be either a String (for object property names) or an Integer (for array indices).- Parameters:
path- The parent JSON path.key- The name or index of the sub-element.quoteStyle- The quoting style for the JSON path string literal.- Returns:
- The JSON path for the sub-element.
-
jsonSubPathAtIndex
Constructs a JSON path for a sub-element at the given array index of the given parent path.- Parameters:
parentPath- The parent JSON path.index- The index of the sub-element.- Returns:
- The JSON path for the sub-element.
- Throws:
IllegalArgumentException- If the given index is negative.
-
jsonSubPathAtName
Constructs a JSON path for a sub-element at the given object property name of the given parent path, using the default quote style (single quotes).- Parameters:
parentPath- The parent JSON path.name- The property name of the sub-element.- Returns:
- The JSON path for the sub-element.
-
jsonSubPathAtName
public static String jsonSubPathAtName(String parentPath, String name, JsonPathUtils.QuoteStyle quoteStyle) Constructs a JSON path for a sub-element at the given object property name of the given parent path, using the given quote style.- Parameters:
parentPath- The parent JSON path.name- The property name of the sub-element.quoteStyle- The quoting style for the JSON path string literal.- Returns:
- The JSON path for the sub-element.
-
readValueAtJsonPath
Returns the value of the object defined by the given JSON path if it exists.- Parameters:
json- to get the value from.pathValue- JSON path defining the location of the value within the given JSON object.- Returns:
- The value of the object defined by the given JSON path if it exists and
nullotherwise.
-
rootPathSegment
Returns the root path segment, i.e., "$".- Returns:
- The root path segment.
-