Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Number comparison in JSONObject::similar can be confused by similar numbers #618

Closed
blalasaadri opened this issue Jul 26, 2021 · 4 comments

Comments

@blalasaadri
Copy link

blalasaadri commented Jul 26, 2021

In version 20210307 I have the following two JSONObjects:

JSONObject first = new JSONObject("{\"a\": 1, \"b\": 2, \"c\": 3}");
JSONObject second = new JSONObject("{\"a\": 1, \"b\": 2.0, \"c\": 4}");

These should not be similar, since the value of c is clearly different. However the value of first.similar(second) will be true because for number fields the logic is

if (valueThis instanceof Number && valueOther instanceof Number) {
    return isNumberSimilar((Number)valueThis, (Number)valueOther);
}

(JSONObject, line 2095) rather than

if (valueThis instanceof Number && valueOther instanceof Number) {
    if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
        return false;
    }
}

so a single number field triggering that comparison will end the loop in which all fields are compared.

(Note: For some objects or on some JVMs, the behaviour may be unpredictable since the order in which the entries are called is not guaranteed to always be the same on a set.)

I'm using the following JDK to test this:

$ java -version
openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
@stleary
Copy link
Owner

stleary commented Jul 26, 2021

Isn't this fixed in #611?

@johnjaylward
Copy link
Contributor

Agree, looks like a duplicate of 611

@stleary
Copy link
Owner

stleary commented Jul 26, 2021

Should be fixed now.

@stleary stleary closed this as completed Jul 26, 2021
@blalasaadri
Copy link
Author

Agreed, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants