Skip to content

Commit

Permalink
Merge branch 'master' into update-copyright
Browse files Browse the repository at this point in the history
  • Loading branch information
stleary committed Aug 27, 2022
2 parents 6daabb4 + 346fb26 commit 89f6e7f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,10 @@ public boolean similar(Object other) {
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
return false;
}
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,10 @@ public boolean similar(Object other) {
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
return false;
}
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
return false;
}
} else if (!valueThis.equals(valueOther)) {
return false;
}
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand All @@ -29,7 +30,9 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONPointerException;
import org.json.JSONString;
import org.json.JSONTokener;
import org.json.junit.data.MyJsonString;
import org.junit.Test;

import com.jayway.jsonpath.Configuration;
Expand Down Expand Up @@ -1334,4 +1337,25 @@ public void issue654StackOverflowInputWellFormed() {
fail("Excepected Exception.");
Util.checkJSONArrayMaps(json_input);
}

@Test
public void testIssue682SimilarityOfJSONString() {
JSONArray ja1 = new JSONArray()
.put(new MyJsonString())
.put(2);
JSONArray ja2 = new JSONArray()
.put(new MyJsonString())
.put(2);
assertTrue(ja1.similar(ja2));

JSONArray ja3 = new JSONArray()
.put(new JSONString() {
@Override
public String toJSONString() {
return "\"different value\"";
}
})
.put(2);
assertFalse(ja1.similar(ja3));
}
}
22 changes: 22 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONPointerException;
import org.json.JSONString;
import org.json.JSONTokener;
import org.json.XML;
import org.json.junit.data.BrokenToString;
Expand Down Expand Up @@ -3487,4 +3488,25 @@ public void issue654StackOverflowInputWellFormed() {
assertNotNull(json_input);
fail("Excepected Exception.");
}

@Test
public void testIssue682SimilarityOfJSONString() {
JSONObject jo1 = new JSONObject()
.put("a", new MyJsonString())
.put("b", 2);
JSONObject jo2 = new JSONObject()
.put("a", new MyJsonString())
.put("b", 2);
assertTrue(jo1.similar(jo2));

JSONObject jo3 = new JSONObject()
.put("a", new JSONString() {
@Override
public String toJSONString() {
return "\"different value\"";
}
})
.put("b", 2);
assertFalse(jo1.similar(jo3));
}
}

0 comments on commit 89f6e7f

Please sign in to comment.