mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 19:35:31 -04:00
successful test
This commit is contained in:
parent
4565bddcbb
commit
1ffcf3915c
@ -366,6 +366,11 @@ public class JSONObject {
|
||||
this.populateMap(bean);
|
||||
}
|
||||
|
||||
private JSONObject(Object bean, Set<Object> objectsRecord) {
|
||||
this();
|
||||
this.populateMap(bean, objectsRecord);
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a JSONObject from an Object, using reflection to find the
|
||||
* public members. The resulting JSONObject's keys will be the strings from
|
||||
@ -1511,11 +1516,6 @@ public class JSONObject {
|
||||
return NULL.equals(object) ? defaultValue : object.toString();
|
||||
}
|
||||
|
||||
// Set to store the current seen objects in the recursive reaversal
|
||||
// If the next value to be added to this set is a duplicate, a cycle
|
||||
// is found
|
||||
private final Set<Object> objectsRecord = new HashSet<Object>();
|
||||
|
||||
/**
|
||||
* Populates the internal map of the JSONObject with the bean properties. The
|
||||
* bean can not be recursive.
|
||||
@ -1526,6 +1526,10 @@ public class JSONObject {
|
||||
* the bean
|
||||
*/
|
||||
private void populateMap(Object bean) {
|
||||
populateMap(bean, new HashSet<Object>());
|
||||
}
|
||||
|
||||
private void populateMap(Object bean, Set<Object> objectsRecord) {
|
||||
Class<?> klass = bean.getClass();
|
||||
|
||||
// If klass is a System class then set includeSuperClass to false.
|
||||
@ -1555,7 +1559,7 @@ public class JSONObject {
|
||||
|
||||
objectsRecord.add(result);
|
||||
|
||||
this.map.put(key, wrap(result));
|
||||
this.map.put(key, wrap(result, objectsRecord));
|
||||
|
||||
objectsRecord.remove(result);
|
||||
|
||||
@ -2449,6 +2453,10 @@ public class JSONObject {
|
||||
* @return The wrapped value
|
||||
*/
|
||||
public static Object wrap(Object object) {
|
||||
return wrap(object, null);
|
||||
}
|
||||
|
||||
private static Object wrap(Object object, Set<Object> objectsRecord) {
|
||||
try {
|
||||
if (NULL.equals(object)) {
|
||||
return NULL;
|
||||
@ -2483,7 +2491,15 @@ public class JSONObject {
|
||||
|| object.getClass().getClassLoader() == null) {
|
||||
return object.toString();
|
||||
}
|
||||
if (objectsRecord != null) {
|
||||
return new JSONObject(object, objectsRecord);
|
||||
}
|
||||
else {
|
||||
return new JSONObject(object);
|
||||
}
|
||||
}
|
||||
catch (JSONException exception) {
|
||||
throw exception;
|
||||
} catch (Exception exception) {
|
||||
return null;
|
||||
}
|
||||
|
@ -73,6 +73,7 @@ import org.json.junit.data.MyJsonString;
|
||||
import org.json.junit.data.MyNumber;
|
||||
import org.json.junit.data.MyNumberContainer;
|
||||
import org.json.junit.data.MyPublicClass;
|
||||
import org.json.junit.data.RecursiveBean;
|
||||
import org.json.junit.data.Singleton;
|
||||
import org.json.junit.data.SingletonEnum;
|
||||
import org.json.junit.data.WeirdList;
|
||||
@ -3218,6 +3219,14 @@ public class JSONObjectTest {
|
||||
jsonObject.put(null, new Object());
|
||||
fail("Expected an exception");
|
||||
}
|
||||
@Test(expected=JSONException.class)
|
||||
public void testSimpleRecursiveObject() {
|
||||
RecursiveBean ObjA = new RecursiveBean("ObjA");
|
||||
RecursiveBean ObjB = new RecursiveBean("ObjB", ObjA);
|
||||
ObjA.setRef(ObjB);
|
||||
JSONObject jsonObject = new JSONObject(ObjA);
|
||||
fail("Expected an exception");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIssue548ObjectWithEmptyJsonArray() {
|
||||
|
@ -10,6 +10,7 @@ public class RecursiveBean {
|
||||
private Object reference;
|
||||
public String getName() { return name; }
|
||||
public Object getRef() {return reference;}
|
||||
public void setRef(Object refObj) {reference = refObj;}
|
||||
|
||||
public RecursiveBean(String name) {
|
||||
this.name = name;
|
||||
|
Loading…
x
Reference in New Issue
Block a user