Class JoltUtils

java.lang.Object
com.bazaarvoice.jolt.utils.JoltUtils

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    cast(Object object)
    Type casts an input object to class indicated by TypeToken
    static <E> E[]
    cast(Object[] object)
    Type cast to array E[]
    static Object
    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(List source, int index)
     
    private static Object
    getOrCreateNextObject(Object source, Object key, Object nextKey)
     
    static boolean
    Given a json document checks if its jst blank doc, i.e.
    static boolean
    Vacant implies there are empty placeholders, i.e.
    static List<Object[]>
    Given a json document, finds out absolute path to every leaf element i.e.
    static List<Object[]>
    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(Object source, 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, Object source, 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, Object source, Object... paths)
    Deprecated.
    static <T> T
    navigateStrict(Object source, Object... paths)
    Navigate a JSON tree (made up of Maps and Lists) to "lookup" the value at a particular path.
    static <T> T
    remove(Object source, 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(Object json, String keyToRemove)
    Removes a key recursively from anywhere in a JSON document.
    static <T> T
    store(Object source, T value, Object... paths)
    For a given non-null (json) object, save the valve in the nested path provided
    static String
    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 Details

    • JoltUtils

      public JoltUtils()
  • Method Details

    • removeRecursive

      public static void removeRecursive(Object json, 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
    • isVacantJson

      public static boolean isVacantJson(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(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 List<Object[]> listKeyChains(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 List<Object[]> listKeyChains(Object key, 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 String toSimpleTraversrPath(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(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(Object[] object)
      Type cast to array E[]
      Parameters:
      object - the input object to cast
      Returns:
      casted array of type E[]
    • compactJson

      public static Object compactJson(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(Object source, T value, 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(Object source, 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(List source, int index)
    • getOrCreateNextObject

      private static Object getOrCreateNextObject(Object source, Object key, Object nextKey)