Package com.bazaarvoice.jolt.utils
Class JoltUtils
java.lang.Object
com.bazaarvoice.jolt.utils.JoltUtils
Handy utilities that do NOT depend on JsonUtil / Jackson live here
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T
Type casts an input object to class indicated by TypeTokenstatic <E> E[]
Type cast to array E[]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 orderprivate static void
ensureListAvailability
(List source, int index) private static Object
getOrCreateNextObject
(Object source, Object key, Object nextKey) static boolean
isBlankJson
(Object obj) Given a json document checks if its jst blank doc, i.e.static boolean
isVacantJson
(Object obj) Vacant implies there are empty placeholders, i.e.listKeyChains
(Object source) Given a json document, finds out absolute path to every leaf element i.e.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: valuestatic <T> T
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
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 ...) insteadstatic void
removeRecursive
(Object json, String keyToRemove) Removes a key recursively from anywhere in a JSON document.static <T> T
For a given non-null (json) object, save the valve in the nested path providedstatic String
toSimpleTraversrPath
(Object[] paths) Converts a standard json path to human readable SimpleTraversr compatible path
-
Constructor Details
-
JoltUtils
public JoltUtils()
-
-
Method Details
-
removeRecursive
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
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
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
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
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
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
Type casts an input object to class indicated by TypeToken- Parameters:
object
- the input object to cast- Returns:
- cast object of type T
-
cast
Type cast to array E[]- Parameters:
object
- the input object to cast- Returns:
- casted array of type E[]
-
compactJson
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
For a given non-null (json) object, save the valve in the nested path provided- Parameters:
source
- the source json objectvalue
- the value to storepaths
- var args Object path to navigate down and store the object in- Returns:
- previously stored value if available, null otherwise
-
remove
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 objectpaths
- var args Object path to navigate down and remove- Returns:
- existing value if available, null otherwise
-
ensureListAvailability
-
getOrCreateNextObject
-