6262 */
6363public class JSONArray implements Iterable <Object >, JSONSimilar {
6464
65+ /**
66+ * The constants used for the method toString to eliminate magic number code smell.
67+ */
68+ private static final int commaMultiplier = 2 ; // Each value requires a comma in output
69+ private static final int minimumBufferSize = 16 ; // Minimum reasonable buffer size for small arrays
70+
6571 /**
6672 * The arrayList where the JSONArray's properties are kept.
6773 */
@@ -1710,11 +1716,11 @@ public String toString() {
17101716
17111717 /**
17121718 * Make a pretty-printed JSON text of this JSONArray.
1713- *
1719+ *
17141720 * <p>If <pre> {@code indentFactor > 0}</pre> and the {@link JSONArray} has only
17151721 * one element, then the array will be output on a single line:
17161722 * <pre>{@code [1]}</pre>
1717- *
1723+ *
17181724 * <p>If an array has 2 or more elements, then it will be output across
17191725 * multiple lines: <pre>{@code
17201726 * [
@@ -1726,9 +1732,8 @@ public String toString() {
17261732 * <p><b>
17271733 * Warning: This method assumes that the data structure is acyclical.
17281734 * </b>
1729- *
1730- * @param indentFactor
1731- * The number of spaces to add to each level of indentation.
1735+ *
1736+ * @param indentFactor The number of spaces to add to each level of indentation.
17321737 * @return a printable, displayable, transmittable representation of the
17331738 * object, beginning with <code>[</code> <small>(left
17341739 * bracket)</small> and ending with <code>]</code>
@@ -1737,25 +1742,18 @@ public String toString() {
17371742 */
17381743 @ SuppressWarnings ("resource" )
17391744 public String toString (int indentFactor ) throws JSONException {
1740- // each value requires a comma, so multiply the count by 2
1741- // We don't want to oversize the initial capacity
1742- int initialSize = myArrayList .size () * 2 ;
1743- Writer sw = new StringBuilderWriter (Math .max (initialSize , 16 ));
1744- return this .write (sw , indentFactor , 0 ).toString ();
1745+ int initialSize = calculateInitialBufferSize (commaMultiplier );
1746+ Writer stringWriter = new StringBuilderWriter (Math .max (initialSize , minimumBufferSize ));
1747+ return this .write (stringWriter , indentFactor , 0 ).toString ();
17451748 }
17461749
17471750 /**
1748- * Write the contents of the JSONArray as JSON text to a writer. For
1749- * compactness, no whitespace is added.
1750- * <p><b>
1751- * Warning: This method assumes that the data structure is acyclical.
1752- *</b>
1753- * @param writer the writer object
1754- * @return The writer.
1755- * @throws JSONException if a called function fails
1751+ * Calculates the initial buffer size needed for JSON string representation.
1752+ * @param elementsPerItem Multiplier accounting for commas between elements
1753+ * @return Calculated initial size based on array contents
17561754 */
1757- public Writer write ( Writer writer ) throws JSONException {
1758- return this . write ( writer , 0 , 0 ) ;
1755+ private int calculateInitialBufferSize ( int elementsPerItem ) {
1756+ return myArrayList . size () * elementsPerItem ;
17591757 }
17601758
17611759 /**
0 commit comments