Class JoltUtils


  • public class JoltUtils
    extends java.lang.Object
    Handy utilities that do NOT depend on JsonUtil / Jackson live here
    • Constructor Summary

      Constructors 
      Constructor Description
      JoltUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> T cast​(java.lang.Object object)
      Type casts an input object to class indicated by TypeToken
      static <E> E[] cast​(java.lang.Object[] object)
      Type cast to array E[]
      static java.lang.Object compactJson​(java.lang.Object source)
      Given a 'fluffy' json document, it recursively removes all null elements to compact the json document Warning: mutates the doc, destroys array order
      private static void ensureListAvailability​(java.util.List source, int index)  
      private static java.lang.Object getOrCreateNextObject​(java.lang.Object source, java.lang.Object key, java.lang.Object nextKey)  
      static boolean isBlankJson​(java.lang.Object obj)
      Given a json document checks if its jst blank doc, i.e.
      static boolean isVacantJson​(java.lang.Object obj)
      Vacant implies there are empty placeholders, i.e.
      static java.util.List<java.lang.Object[]> listKeyChains​(java.lang.Object source)
      Given a json document, finds out absolute path to every leaf element i.e.
      static java.util.List<java.lang.Object[]> listKeyChains​(java.lang.Object key, java.lang.Object value)
      Helper/overridden method for listKeyChain(source), it accepts a key-value pair for convenience note: "key": value (an item in map) and [value] (an item in list) is generalized here as [value] is interpreted in json path as 1: value
      static <T> T navigate​(java.lang.Object source, java.lang.Object... paths)
      Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path.
      static <T> T navigateOrDefault​(T defaultValue, java.lang.Object source, java.lang.Object... paths)
      Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path, but will return the supplied default value if there are any problems.
      static <T> T navigateSafe​(T defaultValue, java.lang.Object source, java.lang.Object... paths)
      Deprecated.
      static <T> T navigateStrict​(java.lang.Object source, java.lang.Object... paths)
      Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path.
      static <T> T remove​(java.lang.Object source, java.lang.Object... paths)
      For a given non-null (json) object, removes and returns the value in the nested path provided Warning: changes array order, to maintain order, use store(source, null, path ...) instead
      static void removeRecursive​(java.lang.Object json, java.lang.String keyToRemove)
      Removes a key recursively from anywhere in a JSON document.
      static <T> T store​(java.lang.Object source, T value, java.lang.Object... paths)
      For a given non-null (json) object, save the valve in the nested path provided
      static java.lang.String toSimpleTraversrPath​(java.lang.Object[] paths)
      Converts a standard json path to human readable SimpleTraversr compatible path
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JoltUtils

        public JoltUtils()
    • Method Detail

      • removeRecursive

        public static void removeRecursive​(java.lang.Object json,
                                           java.lang.String keyToRemove)
        Removes a key recursively from anywhere in a JSON document. NOTE: mutates its input.
        Parameters:
        json - the Jackson Object version of the JSON document (contents changed by this call)
        keyToRemove - the key to remove from the document
      • navigate

        public static <T> T navigate​(java.lang.Object source,
                                     java.lang.Object... paths)
        Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path. Example : given Json Object json = { "a" : { "b" : [ "x", "y", "z" ] } } navigate( json, "a", "b", 0 ) will return "x". It will traverse down the nested "a" and return the zeroth item of the "b" array. You will either get your data, or null. It should never throw an Exception; even if - you ask to index an array with a negative number - you ask to index an array wiht a number bigger than the array size - you ask to index a map that does not exist - your input data has objects in it other than Map, List, String, Number.
        Parameters:
        source - the source JSON object (Map, List, String, Number)
        paths - varargs path you want to travel
        Returns:
        the object of Type at final destination
      • navigateStrict

        public static <T> T navigateStrict​(java.lang.Object source,
                                           java.lang.Object... paths)
                                    throws java.lang.UnsupportedOperationException
        Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path. You will either get your data, or an exception will be thrown. This method should generally only be used in situations where you "know" that the navigate call will "always succeed".
        Parameters:
        source - the source JSON object (Map, List, String, Number)
        paths - varargs path you want to travel
        Returns:
        the object of Type at final destination
        Throws:
        java.lang.UnsupportedOperationException - if there was any problem walking the JSON tree structure
      • navigateOrDefault

        public static <T> T navigateOrDefault​(T defaultValue,
                                              java.lang.Object source,
                                              java.lang.Object... paths)
        Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path, but will return the supplied default value if there are any problems.
        Parameters:
        source - the source JSON object (Map, List, String, Number)
        paths - varargs path you want to travel
        Returns:
        the object of Type at final destination or defaultValue if non existent
      • navigateSafe

        @Deprecated
        public static <T> T navigateSafe​(T defaultValue,
                                         java.lang.Object source,
                                         java.lang.Object... paths)
        Deprecated.
        Use navigateOrDefault which is a much better name.
      • isVacantJson

        public static boolean isVacantJson​(java.lang.Object obj)
        Vacant implies there are empty placeholders, i.e. a vacant hotel Given a json document, checks if it has any "leaf" values, can handle deep nesting of lists and maps i.e. { "a": [ "x": {}, "y": [] ], "b": { "p": [], "q": {} }} ==> is empty
        Parameters:
        obj - source
        Returns:
        true if its an empty json, can have deep nesting, false otherwise
      • isBlankJson

        public static boolean isBlankJson​(java.lang.Object obj)
        Given a json document checks if its jst blank doc, i.e. [] or {}
        Parameters:
        obj - source
        Returns:
        true if the json doc is [] or {}
      • listKeyChains

        public static java.util.List<java.lang.Object[]> listKeyChains​(java.lang.Object source)
        Given a json document, finds out absolute path to every leaf element i.e. { "a": [ "x": { "y": "alpha" }], "b": { "p": [ "beta", "gamma" ], "q": {} }} will yield 1) "a",0,"x","y" -> to "alpha" 2) "b","p", 0 -> to "beta" 3) "b", "p", 1 -> to "gamma" 4) "b","q" -> to {} (empty Map)
        Parameters:
        source - json
        Returns:
        list of Object[] representing path to every leaf element
      • listKeyChains

        public static java.util.List<java.lang.Object[]> listKeyChains​(java.lang.Object key,
                                                                       java.lang.Object value)
        Helper/overridden method for listKeyChain(source), it accepts a key-value pair for convenience note: "key": value (an item in map) and [value] (an item in list) is generalized here as [value] is interpreted in json path as 1: value
        Parameters:
        key -
        value -
        Returns:
        list of Object[] representing path to every leaf element starting with provided root key
      • toSimpleTraversrPath

        public static java.lang.String toSimpleTraversrPath​(java.lang.Object[] paths)
        Converts a standard json path to human readable SimpleTraversr compatible path
        Parameters:
        paths - the path array of objects
        Returns:
        string representation of the path, human readable and SimpleTraversr friendly
      • cast

        public static <T> T cast​(java.lang.Object object)
        Type casts an input object to class indicated by TypeToken
        Parameters:
        object - the input object to cast
        Returns:
        cast object of type T
      • cast

        public static <E> E[] cast​(java.lang.Object[] object)
        Type cast to array E[]
        Parameters:
        object - the input object to cast
        Returns:
        casted array of type E[]
      • compactJson

        public static java.lang.Object compactJson​(java.lang.Object source)
        Given a 'fluffy' json document, it recursively removes all null elements to compact the json document Warning: mutates the doc, destroys array order
        Parameters:
        source -
        Returns:
        mutated source where all null elements are nuked
      • store

        public static <T> T store​(java.lang.Object source,
                                  T value,
                                  java.lang.Object... paths)
        For a given non-null (json) object, save the valve in the nested path provided
        Parameters:
        source - the source json object
        value - the value to store
        paths - var args Object path to navigate down and store the object in
        Returns:
        previously stored value if available, null otherwise
      • remove

        public static <T> T remove​(java.lang.Object source,
                                   java.lang.Object... paths)
        For a given non-null (json) object, removes and returns the value in the nested path provided Warning: changes array order, to maintain order, use store(source, null, path ...) instead
        Parameters:
        source - the source json object
        paths - var args Object path to navigate down and remove
        Returns:
        existing value if available, null otherwise
      • ensureListAvailability

        private static void ensureListAvailability​(java.util.List source,
                                                   int index)
      • getOrCreateNextObject

        private static java.lang.Object getOrCreateNextObject​(java.lang.Object source,
                                                              java.lang.Object key,
                                                              java.lang.Object nextKey)