@@ -116,41 +116,7 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
116116 x .back ();
117117 this .myArrayList .add (x .nextValue ());
118118 }
119- switch (x .nextClean ()) {
120- case 0 :
121- // array is unclosed. No ']' found, instead EOF
122- throw x .syntaxError ("Expected a ',' or ']'" );
123- case ',' :
124- nextChar = x .nextClean ();
125- if (nextChar == 0 ) {
126- // array is unclosed. No ']' found, instead EOF
127- throw x .syntaxError ("Expected a ',' or ']'" );
128- }
129- if (nextChar == ']' ) {
130- // trailing commas are not allowed in strict mode
131- if (jsonParserConfiguration .isStrictMode ()) {
132- throw x .syntaxError ("Strict mode error: Expected another array element" );
133- }
134- return ;
135- }
136- if (nextChar == ',' ) {
137- // consecutive commas are not allowed in strict mode
138- if (jsonParserConfiguration .isStrictMode ()) {
139- throw x .syntaxError ("Strict mode error: Expected a valid array element" );
140- }
141- return ;
142- }
143- x .back ();
144- break ;
145- case ']' :
146- if (isInitial && jsonParserConfiguration .isStrictMode () &&
147- x .nextClean () != 0 ) {
148- throw x .syntaxError ("Strict mode error: Unparsed characters found at end of input text" );
149- }
150- return ;
151- default :
152- throw x .syntaxError ("Expected a ',' or ']'" );
153- }
119+ if (checkForSyntaxError (x , jsonParserConfiguration , isInitial )) return ;
154120 }
155121 } else {
156122 if (isInitial && jsonParserConfiguration .isStrictMode () && x .nextClean () != 0 ) {
@@ -159,6 +125,52 @@ public JSONArray(JSONTokener x, JSONParserConfiguration jsonParserConfiguration)
159125 }
160126 }
161127
128+ /** Convenience function. Checks for JSON syntax error.
129+ * @param x A JSONTokener instance from which the JSONArray is constructed.
130+ * @param jsonParserConfiguration A JSONParserConfiguration instance that controls the behavior of the parser.
131+ * @param isInitial Boolean indicating position of char
132+ * @return
133+ */
134+ private static boolean checkForSyntaxError (JSONTokener x , JSONParserConfiguration jsonParserConfiguration , boolean isInitial ) {
135+ char nextChar ;
136+ switch (x .nextClean ()) {
137+ case 0 :
138+ // array is unclosed. No ']' found, instead EOF
139+ throw x .syntaxError ("Expected a ',' or ']'" );
140+ case ',' :
141+ nextChar = x .nextClean ();
142+ if (nextChar == 0 ) {
143+ // array is unclosed. No ']' found, instead EOF
144+ throw x .syntaxError ("Expected a ',' or ']'" );
145+ }
146+ if (nextChar == ']' ) {
147+ // trailing commas are not allowed in strict mode
148+ if (jsonParserConfiguration .isStrictMode ()) {
149+ throw x .syntaxError ("Strict mode error: Expected another array element" );
150+ }
151+ return true ;
152+ }
153+ if (nextChar == ',' ) {
154+ // consecutive commas are not allowed in strict mode
155+ if (jsonParserConfiguration .isStrictMode ()) {
156+ throw x .syntaxError ("Strict mode error: Expected a valid array element" );
157+ }
158+ return true ;
159+ }
160+ x .back ();
161+ break ;
162+ case ']' :
163+ if (isInitial && jsonParserConfiguration .isStrictMode () &&
164+ x .nextClean () != 0 ) {
165+ throw x .syntaxError ("Strict mode error: Unparsed characters found at end of input text" );
166+ }
167+ return true ;
168+ default :
169+ throw x .syntaxError ("Expected a ',' or ']'" );
170+ }
171+ return false ;
172+ }
173+
162174 /**
163175 * Construct a JSONArray from a source JSON text.
164176 *
@@ -733,11 +745,7 @@ public double optDouble(int index, double defaultValue) {
733745 if (val == null ) {
734746 return defaultValue ;
735747 }
736- final double doubleValue = val .doubleValue ();
737- // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
738- // return defaultValue;
739- // }
740- return doubleValue ;
748+ return val .doubleValue ();
741749 }
742750
743751 /**
@@ -769,11 +777,7 @@ public Double optDoubleObject(int index, Double defaultValue) {
769777 if (val == null ) {
770778 return defaultValue ;
771779 }
772- final Double doubleValue = val .doubleValue ();
773- // if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
774- // return defaultValue;
775- // }
776- return doubleValue ;
780+ return val .doubleValue ();
777781 }
778782
779783 /**
@@ -805,11 +809,7 @@ public float optFloat(int index, float defaultValue) {
805809 if (val == null ) {
806810 return defaultValue ;
807811 }
808- final float floatValue = val .floatValue ();
809- // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
810- // return floatValue;
811- // }
812- return floatValue ;
812+ return val .floatValue ();
813813 }
814814
815815 /**
@@ -841,11 +841,7 @@ public Float optFloatObject(int index, Float defaultValue) {
841841 if (val == null ) {
842842 return defaultValue ;
843843 }
844- final Float floatValue = val .floatValue ();
845- // if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
846- // return floatValue;
847- // }
848- return floatValue ;
844+ return val .floatValue ();
849845 }
850846
851847 /**
@@ -1643,29 +1639,44 @@ public boolean similar(Object other) {
16431639 if (valueThis == null ) {
16441640 return false ;
16451641 }
1646- if (valueThis instanceof JSONObject ) {
1647- if (!((JSONObject )valueThis ).similar (valueOther )) {
1648- return false ;
1649- }
1650- } else if (valueThis instanceof JSONArray ) {
1651- if (!((JSONArray )valueThis ).similar (valueOther )) {
1652- return false ;
1653- }
1654- } else if (valueThis instanceof Number && valueOther instanceof Number ) {
1655- if (!JSONObject .isNumberSimilar ((Number )valueThis , (Number )valueOther )) {
1656- return false ;
1657- }
1658- } else if (valueThis instanceof JSONString && valueOther instanceof JSONString ) {
1659- if (!((JSONString ) valueThis ).toJSONString ().equals (((JSONString ) valueOther ).toJSONString ())) {
1660- return false ;
1661- }
1662- } else if (!valueThis .equals (valueOther )) {
1642+ if (!isSimilar (valueThis , valueOther )) {
16631643 return false ;
16641644 }
16651645 }
16661646 return true ;
16671647 }
16681648
1649+ /**
1650+ * Convenience function; checks for object similarity
1651+ * @param valueThis
1652+ * Initial object to compare
1653+ * @param valueOther
1654+ * Comparison object
1655+ * @return boolean
1656+ */
1657+ private boolean isSimilar (Object valueThis , Object valueOther ) {
1658+ if (valueThis instanceof JSONObject ) {
1659+ if (!((JSONObject )valueThis ).similar (valueOther )) {
1660+ return false ;
1661+ }
1662+ } else if (valueThis instanceof JSONArray ) {
1663+ if (!((JSONArray )valueThis ).similar (valueOther )) {
1664+ return false ;
1665+ }
1666+ } else if (valueThis instanceof Number && valueOther instanceof Number ) {
1667+ if (!JSONObject .isNumberSimilar ((Number )valueThis , (Number )valueOther )) {
1668+ return false ;
1669+ }
1670+ } else if (valueThis instanceof JSONString && valueOther instanceof JSONString ) {
1671+ if (!((JSONString ) valueThis ).toJSONString ().equals (((JSONString ) valueOther ).toJSONString ())) {
1672+ return false ;
1673+ }
1674+ } else if (!valueThis .equals (valueOther )) {
1675+ return false ;
1676+ }
1677+ return true ;
1678+ }
1679+
16691680 /**
16701681 * Produce a JSONObject by combining a JSONArray of names with the values of
16711682 * this JSONArray.
@@ -1797,12 +1808,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
17971808 writer .write ('[' );
17981809
17991810 if (length == 1 ) {
1800- try {
1801- JSONObject .writeValue (writer , this .myArrayList .get (0 ),
1802- indentFactor , indent );
1803- } catch (Exception e ) {
1804- throw new JSONException ("Unable to write JSONArray value at index: 0" , e );
1805- }
1811+ writeArrayAttempt (writer , indentFactor , indent , 0 );
18061812 } else if (length != 0 ) {
18071813 final int newIndent = indent + indentFactor ;
18081814
@@ -1814,12 +1820,7 @@ public Writer write(Writer writer, int indentFactor, int indent)
18141820 writer .write ('\n' );
18151821 }
18161822 JSONObject .indent (writer , newIndent );
1817- try {
1818- JSONObject .writeValue (writer , this .myArrayList .get (i ),
1819- indentFactor , newIndent );
1820- } catch (Exception e ) {
1821- throw new JSONException ("Unable to write JSONArray value at index: " + i , e );
1822- }
1823+ writeArrayAttempt (writer , indentFactor , newIndent , i );
18231824 needsComma = true ;
18241825 }
18251826 if (indentFactor > 0 ) {
@@ -1834,6 +1835,26 @@ public Writer write(Writer writer, int indentFactor, int indent)
18341835 }
18351836 }
18361837
1838+ /**
1839+ * Convenience function. Attempts to write
1840+ * @param writer
1841+ * Writes the serialized JSON
1842+ * @param indentFactor
1843+ * The number of spaces to add to each level of indentation.
1844+ * @param indent
1845+ * The indentation of the top level.
1846+ * @param i
1847+ * Index in array to be added
1848+ */
1849+ private void writeArrayAttempt (Writer writer , int indentFactor , int indent , int i ) {
1850+ try {
1851+ JSONObject .writeValue (writer , this .myArrayList .get (i ),
1852+ indentFactor , indent );
1853+ } catch (Exception e ) {
1854+ throw new JSONException ("Unable to write JSONArray value at index: " + i , e );
1855+ }
1856+ }
1857+
18371858 /**
18381859 * Returns a java.util.List containing all of the elements in this array.
18391860 * If an element in the array is a JSONArray or JSONObject it will also
0 commit comments