Skip to content

Commit d5277b1

Browse files
authored
Merge branch 'stleary:master' into master
2 parents 1a38879 + c4cd526 commit d5277b1

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ public JSONObject(Map<?, ?> m) {
283283
}
284284
final Object value = e.getValue();
285285
if (value != null) {
286+
testValidity(value);
286287
this.map.put(String.valueOf(e.getKey()), wrap(value));
287288
}
288289
}
@@ -346,6 +347,8 @@ public JSONObject(Map<?, ?> m) {
346347
* @param bean
347348
* An object that has getter methods that should be used to make
348349
* a JSONObject.
350+
* @throws JSONException
351+
* If a getter returned a non-finite number.
349352
*/
350353
public JSONObject(Object bean) {
351354
this();
@@ -1691,6 +1694,8 @@ public String optString(String key, String defaultValue) {
16911694
*
16921695
* @param bean
16931696
* the bean
1697+
* @throws JSONException
1698+
* If a getter returned a non-finite number.
16941699
*/
16951700
private void populateMap(Object bean) {
16961701
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
@@ -1726,6 +1731,7 @@ && isValidMethodName(method.getName())) {
17261731

17271732
objectsRecord.add(result);
17281733

1734+
testValidity(result);
17291735
this.map.put(key, wrap(result, objectsRecord));
17301736

17311737
objectsRecord.remove(result);

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static org.junit.Assert.assertNotEquals;
1010
import static org.junit.Assert.assertNotNull;
1111
import static org.junit.Assert.assertNull;
12+
import static org.junit.Assert.assertThrows;
1213
import static org.junit.Assert.assertTrue;
1314
import static org.junit.Assert.fail;
1415
import static org.mockito.Mockito.mock;
@@ -2005,7 +2006,7 @@ public void jsonObjectToStringIndent() {
20052006
@Test
20062007
public void jsonObjectToStringSuppressWarningOnCastToMap() {
20072008
JSONObject jsonObject = new JSONObject();
2008-
Map<String, String> map = new HashMap();
2009+
Map<String, String> map = new HashMap<>();
20092010
map.put("abc", "def");
20102011
jsonObject.put("key", map);
20112012

@@ -3316,7 +3317,7 @@ public void testSingletonEnumBean() {
33163317
@SuppressWarnings("boxing")
33173318
@Test
33183319
public void testGenericBean() {
3319-
GenericBean<Integer> bean = new GenericBean(42);
3320+
GenericBean<Integer> bean = new GenericBean<>(42);
33203321
final JSONObject jo = new JSONObject(bean);
33213322
assertEquals(jo.keySet().toString(), 8, jo.length());
33223323
assertEquals(42, jo.get("genericValue"));
@@ -3660,4 +3661,25 @@ public String toJSONString() {
36603661
.put("b", 2);
36613662
assertFalse(jo1.similar(jo3));
36623663
}
3664+
3665+
private static final Number[] NON_FINITE_NUMBERS = { Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN,
3666+
Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, Float.NaN };
3667+
3668+
@Test
3669+
public void issue713MapConstructorWithNonFiniteNumbers() {
3670+
for (Number nonFinite : NON_FINITE_NUMBERS) {
3671+
Map<String, Number> map = new HashMap<>();
3672+
map.put("a", nonFinite);
3673+
3674+
assertThrows(JSONException.class, () -> new JSONObject(map));
3675+
}
3676+
}
3677+
3678+
@Test
3679+
public void issue713BeanConstructorWithNonFiniteNumbers() {
3680+
for (Number nonFinite : NON_FINITE_NUMBERS) {
3681+
GenericBean<Number> bean = new GenericBean<>(nonFinite);
3682+
assertThrows(JSONException.class, () -> new JSONObject(bean));
3683+
}
3684+
}
36633685
}

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,32 +1223,18 @@ public void testIndentSimpleJsonArray(){
12231223

12241224
@Test
12251225
public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){
1226-
try {
1227-
InputStream jsonStream = null;
1228-
try {
1229-
jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json");
1230-
final JSONObject object = new JSONObject(new JSONTokener(jsonStream));
1231-
String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS,2);
1232-
InputStream xmlStream = null;
1233-
try {
1234-
xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml");
1235-
int bufferSize = 1024;
1236-
char[] buffer = new char[bufferSize];
1237-
StringBuilder expected = new StringBuilder();
1238-
Reader in = new InputStreamReader(xmlStream, "UTF-8");
1239-
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
1240-
expected.append(buffer, 0, numRead);
1241-
}
1242-
assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator")));
1243-
} finally {
1244-
if (xmlStream != null) {
1245-
xmlStream.close();
1246-
}
1247-
}
1248-
} finally {
1249-
if (jsonStream != null) {
1250-
jsonStream.close();
1226+
try (InputStream jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json")) {
1227+
final JSONObject object = new JSONObject(new JSONTokener(jsonStream));
1228+
String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS, 2);
1229+
try (InputStream xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml")) {
1230+
int bufferSize = 1024;
1231+
char[] buffer = new char[bufferSize];
1232+
StringBuilder expected = new StringBuilder();
1233+
Reader in = new InputStreamReader(xmlStream, "UTF-8");
1234+
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
1235+
expected.append(buffer, 0, numRead);
12511236
}
1237+
assertEquals(expected.toString(), actualString);
12521238
}
12531239
} catch (IOException e) {
12541240
fail("file writer error: " +e.getMessage());

src/test/java/org/json/junit/data/GenericBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @param <T>
1010
* generic number value
1111
*/
12-
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
12+
public class GenericBean<T extends Number> implements MyBean {
1313
/**
1414
* @param genericValue
1515
* value to initiate with

0 commit comments

Comments
 (0)