more cleanup sonarqube JSONArray

This commit is contained in:
marilynel 2025-09-21 16:36:52 -08:00
parent f2af220cb4
commit c6efa080c0

View File

@ -116,6 +116,23 @@ public class JSONArray implements Iterable<Object> {
x.back();
this.myArrayList.add(x.nextValue());
}
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
}
} else {
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
}
}
/** Convenience function. Checks for JSON syntax error.
* @param x A JSONTokener instance from which the JSONArray is constructed.
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
* @param isInitial Boolean indicating position of char
* @return
*/
private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
char nextChar;
switch (x.nextClean()) {
case 0:
// array is unclosed. No ']' found, instead EOF
@ -131,14 +148,14 @@ public class JSONArray implements Iterable<Object> {
if (jsonParserConfiguration.isStrictMode()) {
throw x.syntaxError("Strict mode error: Expected another array element");
}
return;
return true;
}
if (nextChar == ',') {
// consecutive commas are not allowed in strict mode
if (jsonParserConfiguration.isStrictMode()) {
throw x.syntaxError("Strict mode error: Expected a valid array element");
}
return;
return true;
}
x.back();
break;
@ -147,16 +164,11 @@ public class JSONArray implements Iterable<Object> {
x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
return;
return true;
default:
throw x.syntaxError("Expected a ',' or ']'");
}
}
} else {
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
}
}
return false;
}
/**
@ -733,11 +745,7 @@ public class JSONArray implements Iterable<Object> {
if (val == null) {
return defaultValue;
}
final double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
return val.doubleValue();
}
/**
@ -769,11 +777,7 @@ public class JSONArray implements Iterable<Object> {
if (val == null) {
return defaultValue;
}
final Double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
return val.doubleValue();
}
/**
@ -805,11 +809,7 @@ public class JSONArray implements Iterable<Object> {
if (val == null) {
return defaultValue;
}
final float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
return val.floatValue();
}
/**
@ -841,11 +841,7 @@ public class JSONArray implements Iterable<Object> {
if (val == null) {
return defaultValue;
}
final Float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
return val.floatValue();
}
/**
@ -1643,6 +1639,22 @@ public class JSONArray implements Iterable<Object> {
if(valueThis == null) {
return false;
}
if (!isSimilar(valueThis, valueOther)) {
return false;
}
}
return true;
}
/**
* Convenience function; checks for object similarity
* @param valueThis
* Initial object to compare
* @param valueOther
* Comparison object
* @return boolean
*/
private boolean isSimilar(Object valueThis, Object valueOther) {
if (valueThis instanceof JSONObject) {
if (!((JSONObject)valueThis).similar(valueOther)) {
return false;
@ -1662,7 +1674,6 @@ public class JSONArray implements Iterable<Object> {
} else if (!valueThis.equals(valueOther)) {
return false;
}
}
return true;
}
@ -1797,12 +1808,7 @@ public class JSONArray implements Iterable<Object> {
writer.write('[');
if (length == 1) {
try {
JSONObject.writeValue(writer, this.myArrayList.get(0),
indentFactor, indent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: 0", e);
}
writeArrayAttempt(writer, indentFactor, indent, 0);
} else if (length != 0) {
final int newIndent = indent + indentFactor;
@ -1814,12 +1820,7 @@ public class JSONArray implements Iterable<Object> {
writer.write('\n');
}
JSONObject.indent(writer, newIndent);
try {
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newIndent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
}
writeArrayAttempt(writer, indentFactor, newIndent, i);
needsComma = true;
}
if (indentFactor > 0) {
@ -1834,6 +1835,26 @@ public class JSONArray implements Iterable<Object> {
}
}
/**
* Convenience function. Attempts to write
* @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 i
* Index in array to be added
*/
private void writeArrayAttempt(Writer writer, int indentFactor, int indent, int i) {
try {
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, indent);
} catch (Exception e) {
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
}
}
/**
* Returns a java.util.List containing all of the elements in this array.
* If an element in the array is a JSONArray or JSONObject it will also