mirror of
https://github.com/stleary/JSON-java.git
synced 2025-10-01 14:25:55 -04:00
Merge pull request #1013 from marilynel/master
sonarqube changes to jsonarray
This commit is contained in:
commit
eb97037f7c
@ -118,6 +118,23 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
x.back();
|
x.back();
|
||||||
this.myArrayList.add(x.nextValue());
|
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()) {
|
switch (x.nextClean()) {
|
||||||
case 0:
|
case 0:
|
||||||
// array is unclosed. No ']' found, instead EOF
|
// array is unclosed. No ']' found, instead EOF
|
||||||
@ -133,14 +150,14 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if (jsonParserConfiguration.isStrictMode()) {
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
throw x.syntaxError("Strict mode error: Expected another array element");
|
throw x.syntaxError("Strict mode error: Expected another array element");
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
if (nextChar == ',') {
|
if (nextChar == ',') {
|
||||||
// consecutive commas are not allowed in strict mode
|
// consecutive commas are not allowed in strict mode
|
||||||
if (jsonParserConfiguration.isStrictMode()) {
|
if (jsonParserConfiguration.isStrictMode()) {
|
||||||
throw x.syntaxError("Strict mode error: Expected a valid array element");
|
throw x.syntaxError("Strict mode error: Expected a valid array element");
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
x.back();
|
x.back();
|
||||||
break;
|
break;
|
||||||
@ -149,16 +166,11 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
x.nextClean() != 0) {
|
x.nextClean() != 0) {
|
||||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
||||||
}
|
}
|
||||||
return;
|
return true;
|
||||||
default:
|
default:
|
||||||
throw x.syntaxError("Expected a ',' or ']'");
|
throw x.syntaxError("Expected a ',' or ']'");
|
||||||
}
|
}
|
||||||
}
|
return false;
|
||||||
} else {
|
|
||||||
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
|
|
||||||
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -735,11 +747,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if (val == null) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
final double doubleValue = val.doubleValue();
|
return val.doubleValue();
|
||||||
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
|
|
||||||
// return defaultValue;
|
|
||||||
// }
|
|
||||||
return doubleValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -771,11 +779,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if (val == null) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
final Double doubleValue = val.doubleValue();
|
return val.doubleValue();
|
||||||
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
|
|
||||||
// return defaultValue;
|
|
||||||
// }
|
|
||||||
return doubleValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -807,11 +811,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if (val == null) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
final float floatValue = val.floatValue();
|
return val.floatValue();
|
||||||
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
|
||||||
// return floatValue;
|
|
||||||
// }
|
|
||||||
return floatValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -843,11 +843,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if (val == null) {
|
if (val == null) {
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
final Float floatValue = val.floatValue();
|
return val.floatValue();
|
||||||
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
|
|
||||||
// return floatValue;
|
|
||||||
// }
|
|
||||||
return floatValue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1645,6 +1641,22 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
if(valueThis == null) {
|
if(valueThis == null) {
|
||||||
return false;
|
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 (valueThis instanceof JSONObject) {
|
||||||
if (!((JSONObject)valueThis).similar(valueOther)) {
|
if (!((JSONObject)valueThis).similar(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
@ -1664,7 +1676,6 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
} else if (!valueThis.equals(valueOther)) {
|
} else if (!valueThis.equals(valueOther)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1799,12 +1810,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
writer.write('[');
|
writer.write('[');
|
||||||
|
|
||||||
if (length == 1) {
|
if (length == 1) {
|
||||||
try {
|
writeArrayAttempt(writer, indentFactor, indent, 0);
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(0),
|
|
||||||
indentFactor, indent);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("Unable to write JSONArray value at index: 0", e);
|
|
||||||
}
|
|
||||||
} else if (length != 0) {
|
} else if (length != 0) {
|
||||||
final int newIndent = indent + indentFactor;
|
final int newIndent = indent + indentFactor;
|
||||||
|
|
||||||
@ -1816,12 +1822,7 @@ public class JSONArray implements Iterable<Object> {
|
|||||||
writer.write('\n');
|
writer.write('\n');
|
||||||
}
|
}
|
||||||
JSONObject.indent(writer, newIndent);
|
JSONObject.indent(writer, newIndent);
|
||||||
try {
|
writeArrayAttempt(writer, indentFactor, newIndent, i);
|
||||||
JSONObject.writeValue(writer, this.myArrayList.get(i),
|
|
||||||
indentFactor, newIndent);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
|
|
||||||
}
|
|
||||||
needsComma = true;
|
needsComma = true;
|
||||||
}
|
}
|
||||||
if (indentFactor > 0) {
|
if (indentFactor > 0) {
|
||||||
@ -1836,6 +1837,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.
|
* 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
|
* If an element in the array is a JSONArray or JSONObject it will also
|
||||||
|
Loading…
x
Reference in New Issue
Block a user