Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 20 additions & 24 deletions src/main/java/org/json/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ public JSONObject(Object object, String ... names) {
try {
this.putOpt(name, c.getField(name).get(object));
} catch (Exception ignore) {
// if invalid, do not include key:value pair in JSONObject
}
}
}
Expand Down Expand Up @@ -651,9 +652,9 @@ public static String doubleToString(double d) {
return "null";
}

// Shave off trailing zeros and decimal point, if possible.

// Shave off trailing zeros and decimal point, if possible.
String string = Double.toString(d);
// idx = 0 case is covered by behavior of Double.toString()
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
Expand Down Expand Up @@ -1130,8 +1131,8 @@ public static String numberToString(Number number) throws JSONException {
testValidity(number);

// Shave off trailing zeros and decimal point, if possible.

String string = number.toString();
// idx = 0 case is covered by behavior of .toString()
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
&& string.indexOf('E') < 0) {
while (string.endsWith("0")) {
Expand Down Expand Up @@ -1397,11 +1398,13 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
}
// don't check if it's a string in case of unchecked Number subclasses
try {
// the other opt functions handle implicit conversions, i.e.
// jo.put("double",1.1d);
// jo.optInt("double"); -- will return 1, not an error
// this conversion to BigDecimal then to BigInteger is to maintain
// that type cast support that may truncate the decimal.
/**
* the other opt functions handle implicit conversions, i.e.
* jo.put("double",1.1d);
* jo.optInt("double"); -- will return 1, not an error
* this conversion to BigDecimal then to BigInteger is to maintain
* that type cast support that may truncate the decimal.
*/
final String valStr = val.toString();
if(isDecimalNotation(valStr)) {
return new BigDecimal(valStr).toBigInteger();
Expand Down Expand Up @@ -1505,11 +1508,7 @@ public float optFloat(String key, float defaultValue) {
if (val == null) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return defaultValue;
// }
return floatValue;
return val.floatValue();
}

/**
Expand Down Expand Up @@ -1541,11 +1540,7 @@ public Float optFloatObject(String key, Float defaultValue) {
if (val == null) {
return defaultValue;
}
final Float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return defaultValue;
// }
return floatValue;
return val.floatValue();
}

/**
Expand Down Expand Up @@ -1916,7 +1911,7 @@ private static String getKeyNameFromMethod(Method method) {
// if the first letter in the key is not uppercase, then skip.
// This is to maintain backwards compatibility before PR406
// (https://github.com/stleary/JSON-java/pull/406/)
if (key.length() == 0 || Character.isLowerCase(key.charAt(0))) {
if (key.isEmpty() || Character.isLowerCase(key.charAt(0))) {
return null;
}
if (key.length() == 1) {
Expand Down Expand Up @@ -1963,6 +1958,7 @@ private static void closeClosable(Object input) {
try {
((Closeable) input).close();
} catch (IOException ignore) {
// close has failed; best effort has been made
}
}
}
Expand All @@ -1982,7 +1978,7 @@ private static void closeClosable(Object input) {
* or one of its super class definitions
*/
private static <A extends Annotation> A getAnnotation(final Method m, final Class<A> annotationClass) {
// if we have invalid data the result is null
// If we have invalid data the result is null
if (m == null || annotationClass == null) {
return null;
}
Expand All @@ -1991,7 +1987,7 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
return m.getAnnotation(annotationClass);
}

// if we've already reached the Object class, return null;
// If we've already reached the Object class, return null;
Class<?> c = m.getDeclaringClass();
if (c.getSuperclass() == null) {
return null;
Expand All @@ -2003,13 +1999,13 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
Method im = i.getMethod(m.getName(), m.getParameterTypes());
return getAnnotation(im, annotationClass);
} catch (final SecurityException ex) {
continue;
// ignore this exception
} catch (final NoSuchMethodException ex) {
continue;
// ignore this excpetion
}
}

//If the superclass is Object, no annotations will be found any more
// If the superclass is Object, no annotations will be found any more
if (Object.class.equals(c.getSuperclass()))
return null;

Expand Down