Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IdentityHashSet for JSONObject cycle detection #651

Merged
merged 1 commit into from Dec 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/main/java/org/json/JSONObject.java
Expand Up @@ -37,9 +37,10 @@ of this software and associated documentation files (the "Software"), to deal
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -1526,7 +1527,7 @@ public String optString(String key, String defaultValue) {
* the bean
*/
private void populateMap(Object bean) {
populateMap(bean, new HashSet<Object>());
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
}

private void populateMap(Object bean, Set<Object> objectsRecord) {
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/json/junit/JSONObjectTest.java
Expand Up @@ -74,6 +74,7 @@ of this software and associated documentation files (the "Software"), to deal
import org.json.junit.data.MyNumberContainer;
import org.json.junit.data.MyPublicClass;
import org.json.junit.data.RecursiveBean;
import org.json.junit.data.RecursiveBeanEquals;
import org.json.junit.data.Singleton;
import org.json.junit.data.SingletonEnum;
import org.json.junit.data.WeirdList;
Expand Down Expand Up @@ -3311,6 +3312,21 @@ public void testLongRepeatObjectNotRecursive() {
new JSONObject(ObjD);
new JSONObject(ObjE);
}
@Test(expected=JSONException.class)
public void testRecursiveEquals() {
RecursiveBeanEquals a = new RecursiveBeanEquals("same");
a.setRef(a);
new JSONObject(a);
}
@Test
public void testNotRecursiveEquals() {
RecursiveBeanEquals a = new RecursiveBeanEquals("same");
RecursiveBeanEquals b = new RecursiveBeanEquals("same");
RecursiveBeanEquals c = new RecursiveBeanEquals("same");
a.setRef(b);
b.setRef(c);
new JSONObject(a);
}


@Test
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/org/json/junit/data/RecursiveBeanEquals.java
@@ -0,0 +1,33 @@
package org.json.junit.data;

/** test class for verifying if recursively defined bean can be correctly identified */
public class RecursiveBeanEquals {
private final String name;
private Object reference;

public RecursiveBeanEquals(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Object getRef() {
return reference;
}

public void setRef(Object refObj) {
reference = refObj;
}

@Override
public boolean equals(Object other) {
return other instanceof RecursiveBeanEquals && name.equals(((RecursiveBeanEquals) other).name);
}

@Override
public int hashCode() {
return name.hashCode();
}
}