Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONObject.similar(JSONObject) what does this really compare?

Tags:

java

json

I was looking for a way to compare two JSONObjects besides using

JSONObject.toString().equals(JSONObject.toString())

and came across this similar method but it's really vague about what it compares.

"Determine if two JSONObjects are similar. They must contain the same set of names which must be associated with similar values." Link

What is that supposed to mean? How similar? Exactly the same?

I searched really hard and couldn't find anything that could clear it up.

like image 946
Chanpuru Avatar asked Mar 06 '17 21:03

Chanpuru


People also ask

How do you compare two JSON objects?

Comparing json is quite simple, we can use '==' operator, Note: '==' and 'is' operator are not same, '==' operator is use to check equality of values , whereas 'is' operator is used to check reference equality, hence one should use '==' operator, 'is' operator will not give expected result.

What is the difference between JSONObject and JSONObject?

JSONObject is "native" to Android SDK, JsonObject is probably the one from Gson library, the one that I use. Two different package, don't work with both ;) choose one. I had some issue with the date formatting in JSONObject.

Can we compare two JSON objects Java?

Compare Two JSON Objects With a Custom Comparatorequals works quite well in most cases. Jackson also provides JsonNode. equals(comparator, JsonNode) to configure a custom Java Comparator object.

How do you compare two JSON objects in Python?

Use json. dumps() and the equal-to operator to compare JSON objects regardless of order. Call json. dumps(json_object, sort_keys) with sort_keys set to True on each json_object to return the object with its key-value pairs sorted in ascending order by the keys.


2 Answers

Here's the source for JSONObject.similar():

/**
 * Determine if two JSONObjects are similar.
 * They must contain the same set of names which must be associated with
 * similar values.
 *
 * @param other The other JSONObject
 * @return true if they are equal
 */
public boolean similar(Object other) {
    try {
        if (!(other instanceof JSONObject)) {
            return false;
        }
        if (!this.keySet().equals(((JSONObject)other).keySet())) {
            return false;
        }
        for (final Entry<String,?> entry : this.entrySet()) {
            String name = entry.getKey();
            Object valueThis = entry.getValue();
            Object valueOther = ((JSONObject)other).get(name);
            if(valueThis == valueOther) {
                continue;
            }
            if(valueThis == null) {
                return false;
            }
            if (valueThis instanceof JSONObject) {
                if (!((JSONObject)valueThis).similar(valueOther)) {
                    return false;
                }
            } else if (valueThis instanceof JSONArray) {
                if (!((JSONArray)valueThis).similar(valueOther)) {
                    return false;
                }
            } else if (valueThis instanceof Number && valueOther instanceof Number) {
                if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
                    return false;
                };
            } else if (!valueThis.equals(valueOther)) {
                return false;
            }
        }
        return true;
    } catch (Throwable exception) {
        return false;
    }
}

Essentially similar() recursively compares the names and values of the JSONObjects and returns true if they're the same. First, it checks if the keySets are equal, then moves on to the values. It recursively checks each value in the keySet to see if they are equal in each JSONObject. If not, it returns false.

like image 159
Harvey Avatar answered Nov 13 '22 08:11

Harvey


The description you found relates to method JSONObject.similar(), which compares if two JSON objects are the same, but having perhaps a different order of its attributes.

The equals() will compare each string caracter, one-by-one checking if it is the same, having the same order.

like image 23
Filipe Andrade Avatar answered Nov 13 '22 07:11

Filipe Andrade