Class JSONCompareUtil


  • public final class JSONCompareUtil
    extends Object
    Utility class that contains Json manipulation methods.
    • Method Detail

      • arrayOfJsonObjectToMap

        public static Map<Object,​org.json.JSONObject> arrayOfJsonObjectToMap​(org.json.JSONArray array,
                                                                                   String uniqueKey)
                                                                            throws org.json.JSONException
        Converts the provided JSONArray to a Map of JSONObjects where the key of each object is the value at uniqueKey in each object.
        Parameters:
        array - the JSON array to convert
        uniqueKey - the key to map the JSON objects to
        Returns:
        the map of JSONObjects from array
        Throws:
        org.json.JSONException - JSON parsing error
      • findUniqueKey

        public static String findUniqueKey​(org.json.JSONArray expected)
                                    throws org.json.JSONException
        Searches for the unique key of the expected JSON 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​(String candidate,
                                                  org.json.JSONArray array)
                                           throws org.json.JSONException

        Looks to see if candidate field is a possible unique key across a array of objects. Returns true IFF:

        1. array is an array of JSONObject
        2. candidate is a top-level field in each of of the objects in the array
        3. candidate is a simple value (not JSONObject or JSONArray)
        4. candidate is unique across all elements in the array
        Parameters:
        candidate - is usable as a unique key if every element in the
        array - 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 List<Object> jsonArrayToList​(org.json.JSONArray expected)
                                            throws org.json.JSONException
        Converts the given JSONArray to a list of Objects.
        Parameters:
        expected - the JSON array to convert
        Returns:
        the list of objects from the expected array
        Throws:
        org.json.JSONException - JSON parsing error
      • getObjectOrNull

        public static Object getObjectOrNull​(org.json.JSONArray jsonArray,
                                             int index)
                                      throws org.json.JSONException
        Returns 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 from
        index - 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.JSONException
        Returns 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 array are simple values
        Throws:
        org.json.JSONException - JSON parsing error
        See Also:
        isSimpleValue(Object)
      • isSimpleValue

        public static boolean isSimpleValue​(Object o)
        Returns whether the given object is a simple value: not JSONObject and not JSONArray.
        Parameters:
        o - the object to inspect
        Returns:
        true if o is a simple value
      • allJSONObjects

        public static boolean allJSONObjects​(org.json.JSONArray array)
                                      throws org.json.JSONException
        Returns whether all elements in array are JSONObject instances.
        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.JSONException
        Returns whether all elements in array are JSONArray instances.
        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 Set<String> getKeys​(org.json.JSONObject jsonObject)
        Collects all keys in jsonObject.
        Parameters:
        jsonObject - the JSONObject to get the keys of
        Returns:
        the set of keys
      • getCardinalityMap

        public static <T> Map<T,​Integer> getCardinalityMap​(Collection<T> coll)
        Creates a cardinality map from coll.
        Type Parameters:
        T - the type of elements in the input collection
        Parameters:
        coll - the collection of items to convert
        Returns:
        the cardinality map