Class JsonPathUtils

    • Method Detail

      • anyPathSegment

        public static String anyPathSegment()
        Returns the any-path-segment, i.e., "*", as per RFC 9535 - Selectors.
        Returns:
        The any-path-segment.
      • encodeForJsonPathDoubleQuotedStringLiteral

        public static String encodeForJsonPathDoubleQuotedStringLiteral​(String name)
        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

        public static String encodeForJsonPathSingleQuotedStringLiteral​(String name)
        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

        public static boolean isJsonPathShorthandMemberName​(String name)
        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

        public static boolean isRootPath​(String jsonPath)
        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

        public static String joinPathSegment​(Object[] segments,
                                             int start,
                                             int end)
      • jsonPathIndexSegment

        public static String jsonPathIndexSegment​(int index)
        Constructs a JSON path index segment for a given array index, as per RFC 9535 - name syntax. For example, given the index 2, 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

        public static String jsonPathNameSegment​(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). For example, given the name foo, this method returns .foo, and for the name foo bar, it returns ['foo bar'].
        Parameters:
        name - A property name.
        Returns:
        The name segment for the given property name.
      • jsonPathNameSegment

        public static String jsonPathNameSegment​(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. For example, given the name foo, this method returns .foo, and for the name foo 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

        public static String jsonPathSegment​(Object segment)
        Returns either jsonPathIndexSegment(int) or jsonPathNameSegment(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

        public static String jsonPathSegment​(Object segment,
                                             JsonPathUtils.QuoteStyle quoteStyle)
        Returns either jsonPathIndexSegment(int) or jsonPathNameSegment(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

        public static String jsonSubPath​(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). 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

        public static String jsonSubPath​(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. 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

        public static String jsonSubPathAtIndex​(String parentPath,
                                                int index)
        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

        public static String jsonSubPathAtName​(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).
        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

        public static Object readValueAtJsonPath​(Map<String,​Object> json,
                                                 Object pathValue)
        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 null otherwise.
      • rootPathSegment

        public static String rootPathSegment()
        Returns the root path segment, i.e., "$".
        Returns:
        The root path segment.