From 9bb26bdb34ba746eb038abc856cf05dafbe4adf4 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 3 Aug 2025 11:52:20 -0800 Subject: [PATCH 1/2] sonar cube stuff --- src/main/java/org/json/JSONObject.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index d852586..1b5161e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -480,6 +480,7 @@ public class JSONObject { try { this.putOpt(name, c.getField(name).get(object)); } catch (Exception ignore) { + // if invalid, do not include key:value pair in JSONObject } } } @@ -651,9 +652,9 @@ public class JSONObject { 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")) { @@ -1130,8 +1131,8 @@ public class JSONObject { 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")) { From 6ed2880f551c120982f196e8872b30316eb91b2e Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 24 Aug 2025 12:55:49 -0800 Subject: [PATCH 2/2] more sonarcube cleanup --- src/main/java/org/json/JSONObject.java | 37 +++++++++++--------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 1b5161e..ad6477a 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1398,11 +1398,13 @@ public class JSONObject { } // 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(); @@ -1506,11 +1508,7 @@ public class JSONObject { if (val == null) { return defaultValue; } - final float floatValue = val.floatValue(); - // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { - // return defaultValue; - // } - return floatValue; + return val.floatValue(); } /** @@ -1542,11 +1540,7 @@ public class JSONObject { if (val == null) { return defaultValue; } - final Float floatValue = val.floatValue(); - // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) { - // return defaultValue; - // } - return floatValue; + return val.floatValue(); } /** @@ -1917,7 +1911,7 @@ public class JSONObject { // 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) { @@ -1964,6 +1958,7 @@ public class JSONObject { try { ((Closeable) input).close(); } catch (IOException ignore) { + // close has failed; best effort has been made } } } @@ -1983,7 +1978,7 @@ public class JSONObject { * or one of its super class definitions */ private static A getAnnotation(final Method m, final Class 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; } @@ -1992,7 +1987,7 @@ public class JSONObject { 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; @@ -2004,13 +1999,13 @@ public class JSONObject { 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;