Class JSONCompareUtil
- java.lang.Object
-
- org.skyscreamer.jsonassert.comparator.JSONCompareUtil
-
public final class JSONCompareUtil extends java.lang.ObjectUtility class that contains Json manipulation methods.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanallJSONArrays(org.json.JSONArray array)Returns whether all elements inarrayareJSONArrayinstances.static booleanallJSONObjects(org.json.JSONArray array)Returns whether all elements inarrayareJSONObjectinstances.static booleanallSimpleValues(org.json.JSONArray array)Returns whether all of the elements in the given array are simple values.static java.util.Map<java.lang.Object,org.json.JSONObject>arrayOfJsonObjectToMap(org.json.JSONArray array, java.lang.String uniqueKey)Converts the providedJSONArrayto a Map ofJSONObjects where the key of each object is the value atuniqueKeyin each object.static java.lang.StringfindUniqueKey(org.json.JSONArray expected)Searches for the unique key of theexpectedJSON array.static java.lang.StringformatUniqueKey(java.lang.String key, java.lang.String uniqueKey, java.lang.Object value)static <T> java.util.Map<T,java.lang.Integer>getCardinalityMap(java.util.Collection<T> coll)Creates a cardinality map fromcoll.static java.util.Set<java.lang.String>getKeys(org.json.JSONObject jsonObject)Collects all keys injsonObject.static java.lang.ObjectgetObjectOrNull(org.json.JSONArray jsonArray, int index)Returns the value present in the given index position.static booleanisSimpleValue(java.lang.Object o)Returns whether the given object is a simple value: notJSONObjectand notJSONArray.static booleanisUsableAsUniqueKey(java.lang.String candidate, org.json.JSONArray array)Looks to see if candidate field is a possible unique key across a array of objects.static java.util.List<java.lang.Object>jsonArrayToList(org.json.JSONArray expected)Converts the givenJSONArrayto a list ofObjects.static java.lang.Stringqualify(java.lang.String prefix, java.lang.String key)
-
-
-
Method Detail
-
arrayOfJsonObjectToMap
public static java.util.Map<java.lang.Object,org.json.JSONObject> arrayOfJsonObjectToMap(org.json.JSONArray array, java.lang.String uniqueKey) throws org.json.JSONExceptionConverts the providedJSONArrayto a Map ofJSONObjects where the key of each object is the value atuniqueKeyin each object.- Parameters:
array- the JSON array to convertuniqueKey- the key to map the JSON objects to- Returns:
- the map of
JSONObjects fromarray - Throws:
org.json.JSONException- JSON parsing error
-
findUniqueKey
public static java.lang.String findUniqueKey(org.json.JSONArray expected) throws org.json.JSONExceptionSearches for the unique key of theexpectedJSON array.- Parameters:
expected- the array to find the unique key of- Returns:
- the unique key if there's any, otherwise null
- Throws:
org.json.JSONException- JSON parsing error
-
isUsableAsUniqueKey
public static boolean isUsableAsUniqueKey(java.lang.String candidate, org.json.JSONArray array) throws org.json.JSONExceptionLooks to see if candidate field is a possible unique key across a array of objects. Returns true IFF:
- array is an array of JSONObject
- candidate is a top-level field in each of of the objects in the array
- candidate is a simple value (not JSONObject or JSONArray)
- candidate is unique across all elements in the array
- Parameters:
candidate- is usable as a unique key if every element in thearray- is a JSONObject having that key, and no two values are the same.- Returns:
- true if the candidate can work as a unique id across array
- Throws:
org.json.JSONException- JSON parsing error
-
jsonArrayToList
public static java.util.List<java.lang.Object> jsonArrayToList(org.json.JSONArray expected) throws org.json.JSONExceptionConverts the givenJSONArrayto a list ofObjects.- Parameters:
expected- the JSON array to convert- Returns:
- the list of objects from the
expectedarray - Throws:
org.json.JSONException- JSON parsing error
-
getObjectOrNull
public static java.lang.Object getObjectOrNull(org.json.JSONArray jsonArray, int index) throws org.json.JSONExceptionReturns the value present in the given index position. If null value is present, it will return null- Parameters:
jsonArray- the JSON array to get value fromindex- index of object to retrieve- Returns:
- value at the given index position
- Throws:
org.json.JSONException- JSON parsing error
-
allSimpleValues
public static boolean allSimpleValues(org.json.JSONArray array) throws org.json.JSONExceptionReturns whether all of the elements in the given array are simple values.- Parameters:
array- the JSON array to iterate through on- Returns:
- true if all the elements in
arrayare simple values - Throws:
org.json.JSONException- JSON parsing error- See Also:
isSimpleValue(Object)
-
isSimpleValue
public static boolean isSimpleValue(java.lang.Object o)
Returns whether the given object is a simple value: notJSONObjectand notJSONArray.- Parameters:
o- the object to inspect- Returns:
- true if
ois a simple value
-
allJSONObjects
public static boolean allJSONObjects(org.json.JSONArray array) throws org.json.JSONExceptionReturns whether all elements inarrayareJSONObjectinstances.- Parameters:
array- the array to inspect- Returns:
- true if all the elements in the given array are JSONObjects
- Throws:
org.json.JSONException- JSON parsing error
-
allJSONArrays
public static boolean allJSONArrays(org.json.JSONArray array) throws org.json.JSONExceptionReturns whether all elements inarrayareJSONArrayinstances.- Parameters:
array- the array to inspect- Returns:
- true if all the elements in the given array are JSONArrays
- Throws:
org.json.JSONException- JSON parsing error
-
getKeys
public static java.util.Set<java.lang.String> getKeys(org.json.JSONObject jsonObject)
Collects all keys injsonObject.- Parameters:
jsonObject- theJSONObjectto get the keys of- Returns:
- the set of keys
-
qualify
public static java.lang.String qualify(java.lang.String prefix, java.lang.String key)
-
formatUniqueKey
public static java.lang.String formatUniqueKey(java.lang.String key, java.lang.String uniqueKey, java.lang.Object value)
-
getCardinalityMap
public static <T> java.util.Map<T,java.lang.Integer> getCardinalityMap(java.util.Collection<T> coll)
Creates a cardinality map fromcoll.- Type Parameters:
T- the type of elements in the input collection- Parameters:
coll- the collection of items to convert- Returns:
- the cardinality map
-
-