X Tutup
Skip to content

Commit 346fb26

Browse files
authored
Merge pull request stleary#683 from spaffrath/Issue_682_JSONString_similarity
Issue 682 JSONString similarity
2 parents 57f785c + 7dd9e01 commit 346fb26

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

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
@@ -2146,6 +2146,10 @@ public boolean similar(Object other) {
21462146
if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) {
21472147
return false;
21482148
}
2149+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
2150+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
2151+
return false;
2152+
}
21492153
} else if (!valueThis.equals(valueOther)) {
21502154
return false;
21512155
}

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;
@@ -1354,4 +1357,25 @@ public void issue654StackOverflowInputWellFormed() {
13541357
fail("Excepected Exception.");
13551358
Util.checkJSONArrayMaps(json_input);
13561359
}
1360+
1361+
@Test
1362+
public void testIssue682SimilarityOfJSONString() {
1363+
JSONArray ja1 = new JSONArray()
1364+
.put(new MyJsonString())
1365+
.put(2);
1366+
JSONArray ja2 = new JSONArray()
1367+
.put(new MyJsonString())
1368+
.put(2);
1369+
assertTrue(ja1.similar(ja2));
1370+
1371+
JSONArray ja3 = new JSONArray()
1372+
.put(new JSONString() {
1373+
@Override
1374+
public String toJSONString() {
1375+
return "\"different value\"";
1376+
}
1377+
})
1378+
.put(2);
1379+
assertFalse(ja1.similar(ja3));
1380+
}
13571381
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ of this software and associated documentation files (the "Software"), to deal
5050
import org.json.JSONException;
5151
import org.json.JSONObject;
5252
import org.json.JSONPointerException;
53+
import org.json.JSONString;
5354
import org.json.JSONTokener;
5455
import org.json.XML;
5556
import org.json.junit.data.BrokenToString;
@@ -3507,4 +3508,25 @@ public void issue654StackOverflowInputWellFormed() {
35073508
assertNotNull(json_input);
35083509
fail("Excepected Exception.");
35093510
}
3511+
3512+
@Test
3513+
public void testIssue682SimilarityOfJSONString() {
3514+
JSONObject jo1 = new JSONObject()
3515+
.put("a", new MyJsonString())
3516+
.put("b", 2);
3517+
JSONObject jo2 = new JSONObject()
3518+
.put("a", new MyJsonString())
3519+
.put("b", 2);
3520+
assertTrue(jo1.similar(jo2));
3521+
3522+
JSONObject jo3 = new JSONObject()
3523+
.put("a", new JSONString() {
3524+
@Override
3525+
public String toJSONString() {
3526+
return "\"different value\"";
3527+
}
3528+
})
3529+
.put("b", 2);
3530+
assertFalse(jo1.similar(jo3));
3531+
}
35103532
}

0 commit comments

Comments
 (0)
X Tutup