Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1386,6 +1386,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 @@ -2142,6 +2142,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 @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal

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 @@ -49,7 +50,9 @@ of this software and associated documentation files (the "Software"), to deal
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 @@ -1298,4 +1301,25 @@ public void issue654StackOverflowInputWellFormed() {
assertNotNull(json_input);
fail("Excepected Exception.");
}

@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 @@ -57,6 +57,7 @@ of this software and associated documentation files (the "Software"), to deal
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 @@ -3398,4 +3399,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));
}
}