Skip to content

Commit 89f16ad

Browse files
committed
Issue 682 JSONString similarity
1 parent 6f92a3a commit 89f16ad

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
@@ -1386,6 +1386,10 @@ public boolean similar(Object other) {
13861386
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
13871387
return false;
13881388
}
1389+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
1390+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
1391+
return false;
1392+
}
13891393
} else if (!valueThis.equals(valueOther)) {
13901394
return false;
13911395
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,10 @@ public boolean similar(Object other) {
21422142
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
21432143
return false;
21442144
}
2145+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
2146+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
2147+
return false;
2148+
}
21452149
} else if (!valueThis.equals(valueOther)) {
21462150
return false;
21472151
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
2626

2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertNotEquals;
2930
import static org.junit.Assert.assertNotNull;
3031
import static org.junit.Assert.assertNull;
3132
import static org.junit.Assert.assertTrue;
@@ -49,7 +50,9 @@ of this software and associated documentation files (the "Software"), to deal
4950
import org.json.JSONException;
5051
import org.json.JSONObject;
5152
import org.json.JSONPointerException;
53+
import org.json.JSONString;
5254
import org.json.JSONTokener;
55+
import org.json.junit.data.MyJsonString;
5356
import org.junit.Test;
5457

5558
import com.jayway.jsonpath.Configuration;
@@ -1298,4 +1301,25 @@ public void issue654StackOverflowInputWellFormed() {
12981301
assertNotNull(json_input);
12991302
fail("Excepected Exception.");
13001303
}
1304+
1305+
@Test
1306+
public void testIssue682SimilarityOfJSONString() {
1307+
JSONArray ja1 = new JSONArray()
1308+
.put(new MyJsonString())
1309+
.put(2);
1310+
JSONArray ja2 = new JSONArray()
1311+
.put(new MyJsonString())
1312+
.put(2);
1313+
assertTrue(ja1.similar(ja2));
1314+
1315+
JSONArray ja3 = new JSONArray()
1316+
.put(new JSONString() {
1317+
@Override
1318+
public String toJSONString() {
1319+
return "\"different value\"";
1320+
}
1321+
})
1322+
.put(2);
1323+
assertFalse(ja1.similar(ja3));
1324+
}
13011325
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ of this software and associated documentation files (the "Software"), to deal
5757
import org.json.JSONException;
5858
import org.json.JSONObject;
5959
import org.json.JSONPointerException;
60+
import org.json.JSONString;
6061
import org.json.JSONTokener;
6162
import org.json.XML;
6263
import org.json.junit.data.BrokenToString;
@@ -3398,4 +3399,25 @@ public void issue654StackOverflowInputWellFormed() {
33983399
assertNotNull(json_input);
33993400
fail("Excepected Exception.");
34003401
}
3402+
3403+
@Test
3404+
public void testIssue682SimilarityOfJSONString() {
3405+
JSONObject jo1 = new JSONObject()
3406+
.put("a", new MyJsonString())
3407+
.put("b", 2);
3408+
JSONObject jo2 = new JSONObject()
3409+
.put("a", new MyJsonString())
3410+
.put("b", 2);
3411+
assertTrue(jo1.similar(jo2));
3412+
3413+
JSONObject jo3 = new JSONObject()
3414+
.put("a", new JSONString() {
3415+
@Override
3416+
public String toJSONString() {
3417+
return "\"different value\"";
3418+
}
3419+
})
3420+
.put("b", 2);
3421+
assertFalse(jo1.similar(jo3));
3422+
}
34013423
}

0 commit comments

Comments
 (0)