From 4e0f62b1a6fe5a5464e026726fabc4a32a97c268 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 7 Sep 2025 12:28:52 -0800 Subject: [PATCH 1/3] more sonarcube optimization in jsonobject.java --- src/main/java/org/json/JSONObject.java | 103 ++++++++++++++----------- 1 file changed, 59 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index ad6477a..cb4e4cf 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1390,7 +1390,7 @@ public class JSONObject { if (!numberIsFinite((Number)val)) { return defaultValue; } - return new BigDecimal(((Number) val).doubleValue()).toBigInteger(); + return BigDecimal.valueOf(((Number) val).doubleValue()).toBigInteger(); } if (val instanceof Long || val instanceof Integer || val instanceof Short || val instanceof Byte){ @@ -2041,7 +2041,7 @@ public class JSONObject { return 1; } - // if we've already reached the Object class, return -1; + // since we've already reached the Object class, return -1; Class c = m.getDeclaringClass(); if (c.getSuperclass() == null) { return -1; @@ -2057,9 +2057,9 @@ public class JSONObject { return d + 1; } } catch (final SecurityException ex) { - continue; + // Nothing to do here } catch (final NoSuchMethodException ex) { - continue; + // Nothing to do here } } @@ -2427,21 +2427,32 @@ public class JSONObject { w.write("\\r"); break; default: - if (c < ' ' || (c >= '\u0080' && c < '\u00a0') - || (c >= '\u2000' && c < '\u2100')) { - w.write("\\u"); - hhhh = Integer.toHexString(c); - w.write("0000", 0, 4 - hhhh.length()); - w.write(hhhh); - } else { - w.write(c); - } + writeAsHex(w, c); } } w.write('"'); return w; } + /** + * Convenience method to reduce cognitive complexity of quote() + * @param w The Writer to which the quoted string will be appended. + * @param c Character to write + * @throws IOException + */ + private static void writeAsHex(Writer w, char c) throws IOException { + String hhhh; + if (c < ' ' || (c >= '\u0080' && c < '\u00a0') + || (c >= '\u2000' && c < '\u2100')) { + w.write("\\u"); + hhhh = Integer.toHexString(c); + w.write("0000", 0, 4 - hhhh.length()); + w.write(hhhh); + } else { + w.write(c); + } + } + /** * Remove a name and its value, if present. * @@ -2470,42 +2481,46 @@ public class JSONObject { if (!this.keySet().equals(((JSONObject)other).keySet())) { return false; } - for (final Entry entry : this.entrySet()) { - String name = entry.getKey(); - Object valueThis = entry.getValue(); - Object valueOther = ((JSONObject)other).get(name); - if(valueThis == valueOther) { - continue; - } - if(valueThis == null) { - return false; - } - if (valueThis instanceof JSONObject) { - if (!((JSONObject)valueThis).similar(valueOther)) { - return false; - } - } else if (valueThis instanceof JSONArray) { - if (!((JSONArray)valueThis).similar(valueOther)) { - return false; - } - } else if (valueThis instanceof Number && valueOther instanceof Number) { - if (!isNumberSimilar((Number)valueThis, (Number)valueOther)) { - return false; - } - } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) { - if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) { - return false; - } - } else if (!valueThis.equals(valueOther)) { - return false; - } - } - return true; + return checkSimilarEntries(other); } catch (Throwable exception) { return false; } } + private boolean checkSimilarEntries(Object other) { + for (final Entry entry : this.entrySet()) { + String name = entry.getKey(); + Object valueThis = entry.getValue(); + Object valueOther = ((JSONObject)other).get(name); + if(valueThis == valueOther) { + continue; + } + if(valueThis == null) { + return false; + } + + if (!checkThis(valueThis, valueOther)) { + return false; + } + } + return true; + } + + private boolean checkThis(Object valueThis, Object valueOther) { + if (valueThis instanceof JSONObject) { + return ((JSONObject)valueThis).similar(valueOther); + } else if (valueThis instanceof JSONArray) { + return ((JSONArray)valueThis).similar(valueOther); + } else if (valueThis instanceof Number && valueOther instanceof Number) { + return isNumberSimilar((Number)valueThis, (Number)valueOther); + } else if (valueThis instanceof JSONString && valueOther instanceof JSONString) { + return ((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString()); + } else if (!valueThis.equals(valueOther)) { + return false; + } + return true; + } + /** * Compares two numbers to see if they are similar. * From 53cfa742a740e278e0928245bf8a20110c6b2ac4 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 7 Sep 2025 12:41:37 -0800 Subject: [PATCH 2/3] more sonarcube optimization in jsonobject.java --- src/main/java/org/json/JSONObject.java | 31 ++++++++++++++++--- .../java/org/json/junit/JSONObjectTest.java | 4 +-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index cb4e4cf..d67ae0e 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -3019,11 +3019,8 @@ public class JSONObject { if (indentFactor > 0) { writer.write(' '); } - try{ - writeValue(writer, entry.getValue(), indentFactor, indent); - } catch (Exception e) { - throw new JSONException("Unable to write JSONObject value for key: " + key, e); - } + // might throw an exception + attemptWriteValue(writer, indentFactor, indent, entry, key); } else if (length != 0) { final int newIndent = indent + indentFactor; for (final Entry entry : this.entrySet()) { @@ -3059,6 +3056,30 @@ public class JSONObject { } } + /** + * Convenience function. Writer attempts to write a value. + * @param writer + * Writes the serialized JSON + * @param indentFactor + * The number of spaces to add to each level of indentation. + * @param indent + * The indentation of the top level. + * @param entry + * Contains the value being written + * @param key + * Identifies the value + * @throws JSONException if a called function has an error or a write error + * occurs + + */ + private static void attemptWriteValue(Writer writer, int indentFactor, int indent, Entry entry, String key) { + try{ + writeValue(writer, entry.getValue(), indentFactor, indent); + } catch (Exception e) { + throw new JSONException("Unable to write JSONObject value for key: " + key, e); + } + } + /** * Returns a java.util.Map containing all of the entries in this object. * If an entry in the object is a JSONArray or JSONObject it will also diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 3c34368..5fff1ed 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -3896,8 +3896,8 @@ public class JSONObjectTest { @Test public void issue743SerializationMapWith1000Objects() { - HashMap map = buildNestedMap(1000); - JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); + HashMap map = buildNestedMap(500); + JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(500); JSONObject object = new JSONObject(map, parserConfiguration); String jsonString = object.toString(); } From 69c87dc4db3dad720a693c153552b7ed93e07769 Mon Sep 17 00:00:00 2001 From: marilynel Date: Sun, 7 Sep 2025 12:52:59 -0800 Subject: [PATCH 3/3] more sonarcube optimization in jsonobject.java --- src/main/java/org/json/JSONObject.java | 62 +++++++++++++++++--------- 1 file changed, 40 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index d67ae0e..ca564a7 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -2926,28 +2926,15 @@ public class JSONObject { if (value == null || value.equals(null)) { writer.write("null"); } else if (value instanceof JSONString) { - // JSONString must be checked first, so it can overwrite behaviour of other types below - Object o; - try { - o = ((JSONString) value).toJSONString(); - } catch (Exception e) { - throw new JSONException(e); - } - writer.write(o != null ? o.toString() : quote(value.toString())); + // may throw an exception + processJsonStringToWriteValue(writer, value); } else if (value instanceof String) { // assuming most values are Strings, so testing it early quote(value.toString(), writer); return writer; } else if (value instanceof Number) { - // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary - final String numberAsString = numberToString((Number) value); - if(NUMBER_PATTERN.matcher(numberAsString).matches()) { - writer.write(numberAsString); - } else { - // The Number value is not a valid JSON number. - // Instead we will quote it as a string - quote(numberAsString, writer); - } + // may throw an exception + processNumberToWriteValue(writer, (Number) value); } else if (value instanceof Boolean) { writer.write(value.toString()); } else if (value instanceof Enum) { @@ -2970,6 +2957,41 @@ public class JSONObject { return writer; } + /** + * Convenience function to reduce cog complexity of calling method; writes value if string is valid + * @param writer Object doing the writing + * @param value Value to be written + * @throws IOException if something goes wrong + */ + private static void processJsonStringToWriteValue(Writer writer, Object value) throws IOException { + // JSONString must be checked first, so it can overwrite behaviour of other types below + Object o; + try { + o = ((JSONString) value).toJSONString(); + } catch (Exception e) { + throw new JSONException(e); + } + writer.write(o != null ? o.toString() : quote(value.toString())); + } + + /** + * Convenience function to reduce cog complexity of calling method; writes value if number is valid + * @param writer Object doing the writing + * @param value Value to be written + * @throws IOException if something goes wrong + */ + private static void processNumberToWriteValue(Writer writer, Number value) throws IOException { + // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary + final String numberAsString = numberToString(value); + if(NUMBER_PATTERN.matcher(numberAsString).matches()) { + writer.write(numberAsString); + } else { + // The Number value is not a valid JSON number. + // Instead we will quote it as a string + quote(numberAsString, writer); + } + } + static final void indent(Writer writer, int indent) throws IOException { for (int i = 0; i < indent; i += 1) { writer.write(' '); @@ -3037,11 +3059,7 @@ public class JSONObject { if (indentFactor > 0) { writer.write(' '); } - try { - writeValue(writer, entry.getValue(), indentFactor, newIndent); - } catch (Exception e) { - throw new JSONException("Unable to write JSONObject value for key: " + key, e); - } + attemptWriteValue(writer, indentFactor, newIndent, entry, key); needsComma = true; } if (indentFactor > 0) {