mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-03 11:25:30 -04:00
add validity check for JSONObject constructors
* fixes #713 * document JSONException in JavaDoc * remove unused Comparable<T> boundary to reuse GenericBean in test
This commit is contained in:
parent
79af389f7a
commit
c93014cb53
@ -283,6 +283,7 @@ public class JSONObject {
|
|||||||
}
|
}
|
||||||
final Object value = e.getValue();
|
final Object value = e.getValue();
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
|
testValidity(value);
|
||||||
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
this.map.put(String.valueOf(e.getKey()), wrap(value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -346,6 +347,8 @@ public class JSONObject {
|
|||||||
* @param bean
|
* @param bean
|
||||||
* An object that has getter methods that should be used to make
|
* An object that has getter methods that should be used to make
|
||||||
* a JSONObject.
|
* a JSONObject.
|
||||||
|
* @throws JSONException
|
||||||
|
* If a getter returned a non-finite number.
|
||||||
*/
|
*/
|
||||||
public JSONObject(Object bean) {
|
public JSONObject(Object bean) {
|
||||||
this();
|
this();
|
||||||
@ -1691,6 +1694,8 @@ public class JSONObject {
|
|||||||
*
|
*
|
||||||
* @param bean
|
* @param bean
|
||||||
* the bean
|
* the bean
|
||||||
|
* @throws JSONException
|
||||||
|
* If a getter returned a non-finite number.
|
||||||
*/
|
*/
|
||||||
private void populateMap(Object bean) {
|
private void populateMap(Object bean) {
|
||||||
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
|
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
|
||||||
@ -1726,6 +1731,7 @@ public class JSONObject {
|
|||||||
|
|
||||||
objectsRecord.add(result);
|
objectsRecord.add(result);
|
||||||
|
|
||||||
|
testValidity(result);
|
||||||
this.map.put(key, wrap(result, objectsRecord));
|
this.map.put(key, wrap(result, objectsRecord));
|
||||||
|
|
||||||
objectsRecord.remove(result);
|
objectsRecord.remove(result);
|
||||||
|
@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse;
|
|||||||
import static org.junit.Assert.assertNotEquals;
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
|
import static org.junit.Assert.assertThrows;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
import static org.junit.Assert.fail;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
@ -1972,7 +1973,7 @@ public class JSONObjectTest {
|
|||||||
@Test
|
@Test
|
||||||
public void jsonObjectToStringSuppressWarningOnCastToMap() {
|
public void jsonObjectToStringSuppressWarningOnCastToMap() {
|
||||||
JSONObject jsonObject = new JSONObject();
|
JSONObject jsonObject = new JSONObject();
|
||||||
Map<String, String> map = new HashMap();
|
Map<String, String> map = new HashMap<>();
|
||||||
map.put("abc", "def");
|
map.put("abc", "def");
|
||||||
jsonObject.put("key", map);
|
jsonObject.put("key", map);
|
||||||
|
|
||||||
@ -3283,7 +3284,7 @@ public class JSONObjectTest {
|
|||||||
@SuppressWarnings("boxing")
|
@SuppressWarnings("boxing")
|
||||||
@Test
|
@Test
|
||||||
public void testGenericBean() {
|
public void testGenericBean() {
|
||||||
GenericBean<Integer> bean = new GenericBean(42);
|
GenericBean<Integer> bean = new GenericBean<>(42);
|
||||||
final JSONObject jo = new JSONObject(bean);
|
final JSONObject jo = new JSONObject(bean);
|
||||||
assertEquals(jo.keySet().toString(), 8, jo.length());
|
assertEquals(jo.keySet().toString(), 8, jo.length());
|
||||||
assertEquals(42, jo.get("genericValue"));
|
assertEquals(42, jo.get("genericValue"));
|
||||||
@ -3627,4 +3628,25 @@ public class JSONObjectTest {
|
|||||||
.put("b", 2);
|
.put("b", 2);
|
||||||
assertFalse(jo1.similar(jo3));
|
assertFalse(jo1.similar(jo3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN,
|
||||||
|
Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN };
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue713MapConstructorWithNonFiniteNumbers() {
|
||||||
|
for (Number nonFinite : NON_FINITE_NUMBERS) {
|
||||||
|
Map<String, Number> map = new HashMap<>();
|
||||||
|
map.put("a", nonFinite);
|
||||||
|
|
||||||
|
assertThrows(JSONException.class, () -> new JSONObject(map));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void issue713BeanConstructorWithNonFiniteNumbers() {
|
||||||
|
for (Number nonFinite : NON_FINITE_NUMBERS) {
|
||||||
|
GenericBean<Number> bean = new GenericBean<>(nonFinite);
|
||||||
|
assertThrows(JSONException.class, () -> new JSONObject(bean));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import java.io.StringReader;
|
|||||||
* @param <T>
|
* @param <T>
|
||||||
* generic number value
|
* generic number value
|
||||||
*/
|
*/
|
||||||
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
|
public class GenericBean<T extends Number> implements MyBean {
|
||||||
/**
|
/**
|
||||||
* @param genericValue
|
* @param genericValue
|
||||||
* value to initiate with
|
* value to initiate with
|
||||||
|
Loading…
x
Reference in New Issue
Block a user