Merge branch 'stleary:master' into master

This commit is contained in:
Rudrajyoti Biswas 2023-10-11 19:17:21 +00:00 committed by GitHub
commit d5277b126b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 28 deletions

View File

@ -283,6 +283,7 @@ public class JSONObject {
}
final Object value = e.getValue();
if (value != null) {
testValidity(value);
this.map.put(String.valueOf(e.getKey()), wrap(value));
}
}
@ -346,6 +347,8 @@ public class JSONObject {
* @param bean
* An object that has getter methods that should be used to make
* a JSONObject.
* @throws JSONException
* If a getter returned a non-finite number.
*/
public JSONObject(Object bean) {
this();
@ -1691,6 +1694,8 @@ public class JSONObject {
*
* @param bean
* the bean
* @throws JSONException
* If a getter returned a non-finite number.
*/
private void populateMap(Object bean) {
populateMap(bean, Collections.newSetFromMap(new IdentityHashMap<Object, Boolean>()));
@ -1726,6 +1731,7 @@ public class JSONObject {
objectsRecord.add(result);
testValidity(result);
this.map.put(key, wrap(result, objectsRecord));
objectsRecord.remove(result);

View File

@ -9,6 +9,7 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
@ -2005,7 +2006,7 @@ public class JSONObjectTest {
@Test
public void jsonObjectToStringSuppressWarningOnCastToMap() {
JSONObject jsonObject = new JSONObject();
Map<String, String> map = new HashMap();
Map<String, String> map = new HashMap<>();
map.put("abc", "def");
jsonObject.put("key", map);
@ -3316,7 +3317,7 @@ public class JSONObjectTest {
@SuppressWarnings("boxing")
@Test
public void testGenericBean() {
GenericBean<Integer> bean = new GenericBean(42);
GenericBean<Integer> bean = new GenericBean<>(42);
final JSONObject jo = new JSONObject(bean);
assertEquals(jo.keySet().toString(), 8, jo.length());
assertEquals(42, jo.get("genericValue"));
@ -3660,4 +3661,25 @@ public class JSONObjectTest {
.put("b", 2);
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));
}
}
}

View File

@ -1223,15 +1223,10 @@ public class XMLTest {
@Test
public void testIndentComplicatedJsonObjectWithArrayAndWithConfig(){
try {
InputStream jsonStream = null;
try {
jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json");
try (InputStream jsonStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.json")) {
final JSONObject object = new JSONObject(new JSONTokener(jsonStream));
String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS,2);
InputStream xmlStream = null;
try {
xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml");
String actualString = XML.toString(object, null, XMLParserConfiguration.KEEP_STRINGS, 2);
try (InputStream xmlStream = XMLTest.class.getClassLoader().getResourceAsStream("Issue593.xml")) {
int bufferSize = 1024;
char[] buffer = new char[bufferSize];
StringBuilder expected = new StringBuilder();
@ -1239,16 +1234,7 @@ public class XMLTest {
for (int numRead; (numRead = in.read(buffer, 0, buffer.length)) > 0; ) {
expected.append(buffer, 0, numRead);
}
assertEquals(expected.toString(), actualString.replaceAll("\\n|\\r\\n", System.getProperty("line.separator")));
} finally {
if (xmlStream != null) {
xmlStream.close();
}
}
} finally {
if (jsonStream != null) {
jsonStream.close();
}
assertEquals(expected.toString(), actualString);
}
} catch (IOException e) {
fail("file writer error: " +e.getMessage());

View File

@ -9,7 +9,7 @@ import java.io.StringReader;
* @param <T>
* generic number value
*/
public class GenericBean<T extends Number & Comparable<T>> implements MyBean {
public class GenericBean<T extends Number> implements MyBean {
/**
* @param genericValue
* value to initiate with