Skip to content

Commit 9b8eefc

Browse files
authored
Merge pull request #1004 from marilynel/master
sonarcube cleanup in JSONObject; more to do
2 parents 78137d3 + 6ed2880 commit 9b8eefc

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

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

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ public JSONObject(Object object, String ... names) {
480480
try {
481481
this.putOpt(name, c.getField(name).get(object));
482482
} catch (Exception ignore) {
483+
// if invalid, do not include key:value pair in JSONObject
483484
}
484485
}
485486
}
@@ -651,9 +652,9 @@ public static String doubleToString(double d) {
651652
return "null";
652653
}
653654

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

11321133
// Shave off trailing zeros and decimal point, if possible.
1133-
11341134
String string = number.toString();
1135+
// idx = 0 case is covered by behavior of .toString()
11351136
if (string.indexOf('.') > 0 && string.indexOf('e') < 0
11361137
&& string.indexOf('E') < 0) {
11371138
while (string.endsWith("0")) {
@@ -1397,11 +1398,13 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
13971398
}
13981399
// don't check if it's a string in case of unchecked Number subclasses
13991400
try {
1400-
// the other opt functions handle implicit conversions, i.e.
1401-
// jo.put("double",1.1d);
1402-
// jo.optInt("double"); -- will return 1, not an error
1403-
// this conversion to BigDecimal then to BigInteger is to maintain
1404-
// that type cast support that may truncate the decimal.
1401+
/**
1402+
* the other opt functions handle implicit conversions, i.e.
1403+
* jo.put("double",1.1d);
1404+
* jo.optInt("double"); -- will return 1, not an error
1405+
* this conversion to BigDecimal then to BigInteger is to maintain
1406+
* that type cast support that may truncate the decimal.
1407+
*/
14051408
final String valStr = val.toString();
14061409
if(isDecimalNotation(valStr)) {
14071410
return new BigDecimal(valStr).toBigInteger();
@@ -1505,11 +1508,7 @@ public float optFloat(String key, float defaultValue) {
15051508
if (val == null) {
15061509
return defaultValue;
15071510
}
1508-
final float floatValue = val.floatValue();
1509-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
1510-
// return defaultValue;
1511-
// }
1512-
return floatValue;
1511+
return val.floatValue();
15131512
}
15141513

15151514
/**
@@ -1541,11 +1540,7 @@ public Float optFloatObject(String key, Float defaultValue) {
15411540
if (val == null) {
15421541
return defaultValue;
15431542
}
1544-
final Float floatValue = val.floatValue();
1545-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
1546-
// return defaultValue;
1547-
// }
1548-
return floatValue;
1543+
return val.floatValue();
15491544
}
15501545

15511546
/**
@@ -1916,7 +1911,7 @@ private static String getKeyNameFromMethod(Method method) {
19161911
// if the first letter in the key is not uppercase, then skip.
19171912
// This is to maintain backwards compatibility before PR406
19181913
// (https://github.com/stleary/JSON-java/pull/406/)
1919-
if (key.length() == 0 || Character.isLowerCase(key.charAt(0))) {
1914+
if (key.isEmpty() || Character.isLowerCase(key.charAt(0))) {
19201915
return null;
19211916
}
19221917
if (key.length() == 1) {
@@ -1963,6 +1958,7 @@ private static void closeClosable(Object input) {
19631958
try {
19641959
((Closeable) input).close();
19651960
} catch (IOException ignore) {
1961+
// close has failed; best effort has been made
19661962
}
19671963
}
19681964
}
@@ -1982,7 +1978,7 @@ private static void closeClosable(Object input) {
19821978
* or one of its super class definitions
19831979
*/
19841980
private static <A extends Annotation> A getAnnotation(final Method m, final Class<A> annotationClass) {
1985-
// if we have invalid data the result is null
1981+
// If we have invalid data the result is null
19861982
if (m == null || annotationClass == null) {
19871983
return null;
19881984
}
@@ -1991,7 +1987,7 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
19911987
return m.getAnnotation(annotationClass);
19921988
}
19931989

1994-
// if we've already reached the Object class, return null;
1990+
// If we've already reached the Object class, return null;
19951991
Class<?> c = m.getDeclaringClass();
19961992
if (c.getSuperclass() == null) {
19971993
return null;
@@ -2003,13 +1999,13 @@ private static <A extends Annotation> A getAnnotation(final Method m, final Clas
20031999
Method im = i.getMethod(m.getName(), m.getParameterTypes());
20042000
return getAnnotation(im, annotationClass);
20052001
} catch (final SecurityException ex) {
2006-
continue;
2002+
// ignore this exception
20072003
} catch (final NoSuchMethodException ex) {
2008-
continue;
2004+
// ignore this excpetion
20092005
}
20102006
}
20112007

2012-
//If the superclass is Object, no annotations will be found any more
2008+
// If the superclass is Object, no annotations will be found any more
20132009
if (Object.class.equals(c.getSuperclass()))
20142010
return null;
20152011

0 commit comments

Comments
 (0)