Skip to content

Commit 89f6e7f

Browse files
committed
Merge branch 'master' into update-copyright
2 parents 6daabb4 + 346fb26 commit 89f6e7f

4 files changed

Lines changed: 54 additions & 0 deletions

File tree

src/main/java/org/json/JSONArray.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,10 @@ public boolean similar(Object other) {
13661366
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
13671367
return false;
13681368
}
1369+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
1370+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
1371+
return false;
1372+
}
13691373
} else if (!valueThis.equals(valueOther)) {
13701374
return false;
13711375
}

src/main/java/org/json/JSONObject.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,6 +2125,10 @@ public boolean similar(Object other) {
21252125
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
21262126
return false;
21272127
}
2128+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
2129+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
2130+
return false;
2131+
}
21282132
} else if (!valueThis.equals(valueOther)) {
21292133
return false;
21302134
}

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static org.junit.Assert.assertEquals;
88
import static org.junit.Assert.assertFalse;
9+
import static org.junit.Assert.assertNotEquals;
910
import static org.junit.Assert.assertNotNull;
1011
import static org.junit.Assert.assertNull;
1112
import static org.junit.Assert.assertTrue;
@@ -29,7 +30,9 @@
2930
import org.json.JSONException;
3031
import org.json.JSONObject;
3132
import org.json.JSONPointerException;
33+
import org.json.JSONString;
3234
import org.json.JSONTokener;
35+
import org.json.junit.data.MyJsonString;
3336
import org.junit.Test;
3437

3538
import com.jayway.jsonpath.Configuration;
@@ -1334,4 +1337,25 @@ public void issue654StackOverflowInputWellFormed() {
13341337
fail("Excepected Exception.");
13351338
Util.checkJSONArrayMaps(json_input);
13361339
}
1340+
1341+
@Test
1342+
public void testIssue682SimilarityOfJSONString() {
1343+
JSONArray ja1 = new JSONArray()
1344+
.put(new MyJsonString())
1345+
.put(2);
1346+
JSONArray ja2 = new JSONArray()
1347+
.put(new MyJsonString())
1348+
.put(2);
1349+
assertTrue(ja1.similar(ja2));
1350+
1351+
JSONArray ja3 = new JSONArray()
1352+
.put(new JSONString() {
1353+
@Override
1354+
public String toJSONString() {
1355+
return "\"different value\"";
1356+
}
1357+
})
1358+
.put(2);
1359+
assertFalse(ja1.similar(ja3));
1360+
}
13371361
}

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.json.JSONException;
3131
import org.json.JSONObject;
3232
import org.json.JSONPointerException;
33+
import org.json.JSONString;
3334
import org.json.JSONTokener;
3435
import org.json.XML;
3536
import org.json.junit.data.BrokenToString;
@@ -3487,4 +3488,25 @@ public void issue654StackOverflowInputWellFormed() {
34873488
assertNotNull(json_input);
34883489
fail("Excepected Exception.");
34893490
}
3491+
3492+
@Test
3493+
public void testIssue682SimilarityOfJSONString() {
3494+
JSONObject jo1 = new JSONObject()
3495+
.put("a", new MyJsonString())
3496+
.put("b", 2);
3497+
JSONObject jo2 = new JSONObject()
3498+
.put("a", new MyJsonString())
3499+
.put("b", 2);
3500+
assertTrue(jo1.similar(jo2));
3501+
3502+
JSONObject jo3 = new JSONObject()
3503+
.put("a", new JSONString() {
3504+
@Override
3505+
public String toJSONString() {
3506+
return "\"different value\"";
3507+
}
3508+
})
3509+
.put("b", 2);
3510+
assertFalse(jo1.similar(jo3));
3511+
}
34903512
}

0 commit comments

Comments
 (0)