Skip to content

Commit 04e8ea8

Browse files
authored
Merge pull request #651 from cushon/i650
Use IdentityHashSet for cycle detection
2 parents bc623e3 + 812955e commit 04e8ea8

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ of this software and associated documentation files (the "Software"), to deal
3737
import java.math.BigDecimal;
3838
import java.math.BigInteger;
3939
import java.util.Collection;
40+
import java.util.Collections;
4041
import java.util.Enumeration;
4142
import java.util.HashMap;
42-
import java.util.HashSet;
43+
import java.util.IdentityHashMap;
4344
import java.util.Iterator;
4445
import java.util.Locale;
4546
import java.util.Map;
@@ -1526,7 +1527,7 @@ public String optString(String key, String defaultValue) {
15261527
* the bean
15271528
*/
15281529
private void populateMap(Object bean) {
1529-
populateMap(bean, new HashSet<Object>());
1530+
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
15301531
}
15311532

15321533
private void populateMap(Object bean, Set<Object> objectsRecord) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ of this software and associated documentation files (the "Software"), to deal
7474
import org.json.junit.data.MyNumberContainer;
7575
import org.json.junit.data.MyPublicClass;
7676
import org.json.junit.data.RecursiveBean;
77+
import org.json.junit.data.RecursiveBeanEquals;
7778
import org.json.junit.data.Singleton;
7879
import org.json.junit.data.SingletonEnum;
7980
import org.json.junit.data.WeirdList;
@@ -3311,6 +3312,21 @@ public void testLongRepeatObjectNotRecursive() {
33113312
new JSONObject(ObjD);
33123313
new JSONObject(ObjE);
33133314
}
3315+
@Test(expected=JSONException.class)
3316+
public void testRecursiveEquals() {
3317+
RecursiveBeanEquals a = new RecursiveBeanEquals("same");
3318+
a.setRef(a);
3319+
new JSONObject(a);
3320+
}
3321+
@Test
3322+
public void testNotRecursiveEquals() {
3323+
RecursiveBeanEquals a = new RecursiveBeanEquals("same");
3324+
RecursiveBeanEquals b = new RecursiveBeanEquals("same");
3325+
RecursiveBeanEquals c = new RecursiveBeanEquals("same");
3326+
a.setRef(b);
3327+
b.setRef(c);
3328+
new JSONObject(a);
3329+
}
33143330

33153331

33163332
@Test
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package org.json.junit.data;
2+
3+
/** test class for verifying if recursively defined bean can be correctly identified */
4+
public class RecursiveBeanEquals {
5+
private final String name;
6+
private Object reference;
7+
8+
public RecursiveBeanEquals(String name) {
9+
this.name = name;
10+
}
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public Object getRef() {
17+
return reference;
18+
}
19+
20+
public void setRef(Object refObj) {
21+
reference = refObj;
22+
}
23+
24+
@Override
25+
public boolean equals(Object other) {
26+
return other instanceof RecursiveBeanEquals && name.equals(((RecursiveBeanEquals) other).name);
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return name.hashCode();
32+
}
33+
}

0 commit comments

Comments
 (0)