public class JoltUtils
extends java.lang.Object
Constructor and Description |
---|
JoltUtils() |
Modifier and Type | Method and 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
|
public static void removeRecursive(java.lang.Object json, java.lang.String keyToRemove)
json
- the Jackson Object version of the JSON document
(contents changed by this call)keyToRemove
- the key to remove from the documentpublic static <T> T navigate(java.lang.Object source, java.lang.Object... paths)
source
- the source JSON object (Map, List, String, Number)paths
- varargs path you want to travelpublic static <T> T navigateStrict(java.lang.Object source, java.lang.Object... paths) throws java.lang.UnsupportedOperationException
source
- the source JSON object (Map, List, String, Number)paths
- varargs path you want to traveljava.lang.UnsupportedOperationException
- if there was any problem walking the JSON tree structurepublic static <T> T navigateOrDefault(T defaultValue, java.lang.Object source, java.lang.Object... paths)
source
- the source JSON object (Map, List, String, Number)paths
- varargs path you want to travel@Deprecated public static <T> T navigateSafe(T defaultValue, java.lang.Object source, java.lang.Object... paths)
public static boolean isVacantJson(java.lang.Object obj)
obj
- sourcepublic static boolean isBlankJson(java.lang.Object obj)
obj
- sourcepublic static java.util.List<java.lang.Object[]> listKeyChains(java.lang.Object source)
source
- jsonpublic static java.util.List<java.lang.Object[]> listKeyChains(java.lang.Object key, java.lang.Object value)
key
- value
- public static java.lang.String toSimpleTraversrPath(java.lang.Object[] paths)
paths
- the path array of objectspublic static <T> T cast(java.lang.Object object)
object
- the input object to castpublic static <E> E[] cast(java.lang.Object[] object)
object
- the input object to castpublic static java.lang.Object compactJson(java.lang.Object source)
source
- public static <T> T store(java.lang.Object source, T value, java.lang.Object... paths)
source
- the source json objectvalue
- the value to storepaths
- var args Object path to navigate down and store the object inpublic static <T> T remove(java.lang.Object source, java.lang.Object... paths)
source
- the source json objectpaths
- var args Object path to navigate down and removeprivate 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)