Merge pull request #1005 from marilynel/master

fixing sonar cube issues
This commit is contained in:
Sean Leary 2025-09-11 15:42:48 -05:00 committed by GitHub
commit a3edc1da0f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 127 additions and 73 deletions

View File

@ -1390,7 +1390,7 @@ public class JSONObject {
if (!numberIsFinite((Number)val)) { if (!numberIsFinite((Number)val)) {
return defaultValue; return defaultValue;
} }
return new BigDecimal(((Number) val).doubleValue()).toBigInteger(); return BigDecimal.valueOf(((Number) val).doubleValue()).toBigInteger();
} }
if (val instanceof Long || val instanceof Integer if (val instanceof Long || val instanceof Integer
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
@ -2041,7 +2041,7 @@ public class JSONObject {
return 1; 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(); Class<?> c = m.getDeclaringClass();
if (c.getSuperclass() == null) { if (c.getSuperclass() == null) {
return -1; return -1;
@ -2057,9 +2057,9 @@ public class JSONObject {
return d + 1; return d + 1;
} }
} catch (final SecurityException ex) { } catch (final SecurityException ex) {
continue; // Nothing to do here
} catch (final NoSuchMethodException ex) { } catch (final NoSuchMethodException ex) {
continue; // Nothing to do here
} }
} }
@ -2427,21 +2427,32 @@ public class JSONObject {
w.write("\\r"); w.write("\\r");
break; break;
default: default:
if (c < ' ' || (c >= '\u0080' && c < '\u00a0') writeAsHex(w, c);
|| (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);
}
} }
} }
w.write('"'); w.write('"');
return w; 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. * Remove a name and its value, if present.
* *
@ -2470,42 +2481,46 @@ public class JSONObject {
if (!this.keySet().equals(((JSONObject)other).keySet())) { if (!this.keySet().equals(((JSONObject)other).keySet())) {
return false; return false;
} }
for (final Entry<String,?> entry : this.entrySet()) { return checkSimilarEntries(other);
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;
} catch (Throwable exception) { } catch (Throwable exception) {
return false; return false;
} }
} }
private boolean checkSimilarEntries(Object other) {
for (final Entry<String,?> 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. * Compares two numbers to see if they are similar.
* *
@ -2911,28 +2926,15 @@ public class JSONObject {
if (value == null || value.equals(null)) { if (value == null || value.equals(null)) {
writer.write("null"); writer.write("null");
} else if (value instanceof JSONString) { } else if (value instanceof JSONString) {
// JSONString must be checked first, so it can overwrite behaviour of other types below // may throw an exception
Object o; processJsonStringToWriteValue(writer, value);
try {
o = ((JSONString) value).toJSONString();
} catch (Exception e) {
throw new JSONException(e);
}
writer.write(o != null ? o.toString() : quote(value.toString()));
} else if (value instanceof String) { } else if (value instanceof String) {
// assuming most values are Strings, so testing it early // assuming most values are Strings, so testing it early
quote(value.toString(), writer); quote(value.toString(), writer);
return writer; return writer;
} else if (value instanceof Number) { } else if (value instanceof Number) {
// not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary // may throw an exception
final String numberAsString = numberToString((Number) value); processNumberToWriteValue(writer, (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);
}
} else if (value instanceof Boolean) { } else if (value instanceof Boolean) {
writer.write(value.toString()); writer.write(value.toString());
} else if (value instanceof Enum<?>) { } else if (value instanceof Enum<?>) {
@ -2955,6 +2957,41 @@ public class JSONObject {
return writer; 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 { static final void indent(Writer writer, int indent) throws IOException {
for (int i = 0; i < indent; i += 1) { for (int i = 0; i < indent; i += 1) {
writer.write(' '); writer.write(' ');
@ -3004,11 +3041,8 @@ public class JSONObject {
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write(' '); writer.write(' ');
} }
try{ // might throw an exception
writeValue(writer, entry.getValue(), indentFactor, indent); attemptWriteValue(writer, indentFactor, indent, entry, key);
} catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
} else if (length != 0) { } else if (length != 0) {
final int newIndent = indent + indentFactor; final int newIndent = indent + indentFactor;
for (final Entry<String,?> entry : this.entrySet()) { for (final Entry<String,?> entry : this.entrySet()) {
@ -3025,11 +3059,7 @@ public class JSONObject {
if (indentFactor > 0) { if (indentFactor > 0) {
writer.write(' '); writer.write(' ');
} }
try { attemptWriteValue(writer, indentFactor, newIndent, entry, key);
writeValue(writer, entry.getValue(), indentFactor, newIndent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONObject value for key: " + key, e);
}
needsComma = true; needsComma = true;
} }
if (indentFactor > 0) { if (indentFactor > 0) {
@ -3044,6 +3074,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<String, ?> 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. * 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 * If an entry in the object is a JSONArray or JSONObject it will also

View File

@ -3896,8 +3896,8 @@ public class JSONObjectTest {
@Test @Test
public void issue743SerializationMapWith1000Objects() { public void issue743SerializationMapWith1000Objects() {
HashMap<String, Object> map = buildNestedMap(1000); HashMap<String, Object> map = buildNestedMap(500);
JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(1000); JSONParserConfiguration parserConfiguration = new JSONParserConfiguration().withMaxNestingDepth(500);
JSONObject object = new JSONObject(map, parserConfiguration); JSONObject object = new JSONObject(map, parserConfiguration);
String jsonString = object.toString(); String jsonString = object.toString();
} }