Skip to content

Commit eb97037

Browse files
authored
Merge pull request #1013 from marilynel/master
sonarqube changes to jsonarray
2 parents aff59d0 + 05867c4 commit eb97037

File tree

1 file changed

+105
-84
lines changed

1 file changed

+105
-84
lines changed

src/main/java/org/json/JSONArray.java

Lines changed: 105 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -118,41 +118,7 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
118118
x.back();
119119
this.myArrayList.add(x.nextValue());
120120
}
121-
switch (x.nextClean()) {
122-
case 0:
123-
// array is unclosed. No ']' found, instead EOF
124-
throw x.syntaxError("Expected a ',' or ']'");
125-
case ',':
126-
nextChar = x.nextClean();
127-
if (nextChar == 0) {
128-
// array is unclosed. No ']' found, instead EOF
129-
throw x.syntaxError("Expected a ',' or ']'");
130-
}
131-
if (nextChar == ']') {
132-
// trailing commas are not allowed in strict mode
133-
if (jsonParserConfiguration.isStrictMode()) {
134-
throw x.syntaxError("Strict mode error: Expected another array element");
135-
}
136-
return;
137-
}
138-
if (nextChar == ',') {
139-
// consecutive commas are not allowed in strict mode
140-
if (jsonParserConfiguration.isStrictMode()) {
141-
throw x.syntaxError("Strict mode error: Expected a valid array element");
142-
}
143-
return;
144-
}
145-
x.back();
146-
break;
147-
case ']':
148-
if (isInitial && jsonParserConfiguration.isStrictMode() &&
149-
x.nextClean() != 0) {
150-
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
151-
}
152-
return;
153-
default:
154-
throw x.syntaxError("Expected a ',' or ']'");
155-
}
121+
if (checkForSyntaxError(x, jsonParserConfiguration, isInitial)) return;
156122
}
157123
} else {
158124
if (isInitial && jsonParserConfiguration.isStrictMode() && x.nextClean() != 0) {
@@ -161,6 +127,52 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
161127
}
162128
}
163129

130+
/** Convenience function. Checks for JSON syntax error.
131+
* @param x A JSONTokener instance from which the JSONArray is constructed.
132+
* @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
133+
* @param isInitial Boolean indicating position of char
134+
* @return
135+
*/
136+
private static boolean checkForSyntaxError(JSONTokener x, JSONParserConfiguration jsonParserConfiguration, boolean isInitial) {
137+
char nextChar;
138+
switch (x.nextClean()) {
139+
case 0:
140+
// array is unclosed. No ']' found, instead EOF
141+
throw x.syntaxError("Expected a ',' or ']'");
142+
case ',':
143+
nextChar = x.nextClean();
144+
if (nextChar == 0) {
145+
// array is unclosed. No ']' found, instead EOF
146+
throw x.syntaxError("Expected a ',' or ']'");
147+
}
148+
if (nextChar == ']') {
149+
// trailing commas are not allowed in strict mode
150+
if (jsonParserConfiguration.isStrictMode()) {
151+
throw x.syntaxError("Strict mode error: Expected another array element");
152+
}
153+
return true;
154+
}
155+
if (nextChar == ',') {
156+
// consecutive commas are not allowed in strict mode
157+
if (jsonParserConfiguration.isStrictMode()) {
158+
throw x.syntaxError("Strict mode error: Expected a valid array element");
159+
}
160+
return true;
161+
}
162+
x.back();
163+
break;
164+
case ']':
165+
if (isInitial && jsonParserConfiguration.isStrictMode() &&
166+
x.nextClean() != 0) {
167+
throw x.syntaxError("Strict mode error: Unparsed characters found at end of input text");
168+
}
169+
return true;
170+
default:
171+
throw x.syntaxError("Expected a ',' or ']'");
172+
}
173+
return false;
174+
}
175+
164176
/**
165177
* Construct a JSONArray from a source JSON text.
166178
*
@@ -735,11 +747,7 @@ public double optDouble(int index, double defaultValue) {
735747
if (val == null) {
736748
return defaultValue;
737749
}
738-
final double doubleValue = val.doubleValue();
739-
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
740-
// return defaultValue;
741-
// }
742-
return doubleValue;
750+
return val.doubleValue();
743751
}
744752

745753
/**
@@ -771,11 +779,7 @@ public Double optDoubleObject(int index, Double defaultValue) {
771779
if (val == null) {
772780
return defaultValue;
773781
}
774-
final Double doubleValue = val.doubleValue();
775-
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
776-
// return defaultValue;
777-
// }
778-
return doubleValue;
782+
return val.doubleValue();
779783
}
780784

781785
/**
@@ -807,11 +811,7 @@ public float optFloat(int index, float defaultValue) {
807811
if (val == null) {
808812
return defaultValue;
809813
}
810-
final float floatValue = val.floatValue();
811-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
812-
// return floatValue;
813-
// }
814-
return floatValue;
814+
return val.floatValue();
815815
}
816816

817817
/**
@@ -843,11 +843,7 @@ public Float optFloatObject(int index, Float defaultValue) {
843843
if (val == null) {
844844
return defaultValue;
845845
}
846-
final Float floatValue = val.floatValue();
847-
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
848-
// return floatValue;
849-
// }
850-
return floatValue;
846+
return val.floatValue();
851847
}
852848

853849
/**
@@ -1645,29 +1641,44 @@ public boolean similar(Object other) {
16451641
if(valueThis == null) {
16461642
return false;
16471643
}
1648-
if (valueThis instanceof JSONObject) {
1649-
if (!((JSONObject)valueThis).similar(valueOther)) {
1650-
return false;
1651-
}
1652-
} else if (valueThis instanceof JSONArray) {
1653-
if (!((JSONArray)valueThis).similar(valueOther)) {
1654-
return false;
1655-
}
1656-
} else if (valueThis instanceof Number && valueOther instanceof Number) {
1657-
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
1658-
return false;
1659-
}
1660-
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
1661-
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
1662-
return false;
1663-
}
1664-
} else if (!valueThis.equals(valueOther)) {
1644+
if (!isSimilar(valueThis, valueOther)) {
16651645
return false;
16661646
}
16671647
}
16681648
return true;
16691649
}
16701650

1651+
/**
1652+
* Convenience function; checks for object similarity
1653+
* @param valueThis
1654+
* Initial object to compare
1655+
* @param valueOther
1656+
* Comparison object
1657+
* @return boolean
1658+
*/
1659+
private boolean isSimilar(Object valueThis, Object valueOther) {
1660+
if (valueThis instanceof JSONObject) {
1661+
if (!((JSONObject)valueThis).similar(valueOther)) {
1662+
return false;
1663+
}
1664+
} else if (valueThis instanceof JSONArray) {
1665+
if (!((JSONArray)valueThis).similar(valueOther)) {
1666+
return false;
1667+
}
1668+
} else if (valueThis instanceof Number && valueOther instanceof Number) {
1669+
if (!JSONObject.isNumberSimilar((Number)valueThis, (Number)valueOther)) {
1670+
return false;
1671+
}
1672+
} else if (valueThis instanceof JSONString && valueOther instanceof JSONString) {
1673+
if (!((JSONString) valueThis).toJSONString().equals(((JSONString) valueOther).toJSONString())) {
1674+
return false;
1675+
}
1676+
} else if (!valueThis.equals(valueOther)) {
1677+
return false;
1678+
}
1679+
return true;
1680+
}
1681+
16711682
/**
16721683
* Produce a JSONObject by combining a JSONArray of names with the values of
16731684
* this JSONArray.
@@ -1799,12 +1810,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
17991810
writer.write('[');
18001811

18011812
if (length == 1) {
1802-
try {
1803-
JSONObject.writeValue(writer, this.myArrayList.get(0),
1804-
indentFactor, indent);
1805-
} catch (Exception e) {
1806-
throw new JSONException("Unable to write JSONArray value at index: 0", e);
1807-
}
1813+
writeArrayAttempt(writer, indentFactor, indent, 0);
18081814
} else if (length != 0) {
18091815
final int newIndent = indent + indentFactor;
18101816

@@ -1816,12 +1822,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
18161822
writer.write('\n');
18171823
}
18181824
JSONObject.indent(writer, newIndent);
1819-
try {
1820-
JSONObject.writeValue(writer, this.myArrayList.get(i),
1821-
indentFactor, newIndent);
1822-
} catch (Exception e) {
1823-
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
1824-
}
1825+
writeArrayAttempt(writer, indentFactor, newIndent, i);
18251826
needsComma = true;
18261827
}
18271828
if (indentFactor > 0) {
@@ -1836,6 +1837,26 @@ public Writer write(Writer writer, int indentFactor, int indent)
18361837
}
18371838
}
18381839

1840+
/**
1841+
* Convenience function. Attempts to write
1842+
* @param writer
1843+
* Writes the serialized JSON
1844+
* @param indentFactor
1845+
* The number of spaces to add to each level of indentation.
1846+
* @param indent
1847+
* The indentation of the top level.
1848+
* @param i
1849+
* Index in array to be added
1850+
*/
1851+
private void writeArrayAttempt(Writer writer, int indentFactor, int indent, int i) {
1852+
try {
1853+
JSONObject.writeValue(writer, this.myArrayList.get(i),
1854+
indentFactor, indent);
1855+
} catch (Exception e) {
1856+
throw new JSONException("Unable to write JSONArray value at index: " + i, e);
1857+
}
1858+
}
1859+
18391860
/**
18401861
* Returns a java.util.List containing all of the elements in this array.
18411862
* If an element in the array is a JSONArray or JSONObject it will also

0 commit comments

Comments
 (0)