Skip to content

Commit 4af5af9

Browse files
author
alwinsanil
committed
Bug fixes
2 parents 3c34351 + 84332f3 commit 4af5af9

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

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

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@
6262
*/
6363
public class JSONArray implements Iterable<Object>, JSONSimilar {
6464

65-
/**
66-
* The constants used for the method toString to eliminate magic number code smell.
67-
*/
6865
private static final int commaMultiplier = 2; // Each value requires a comma in output
6966
private static final int minimumBufferSize = 16; // Minimum reasonable buffer size for small arrays
7067

@@ -1716,11 +1713,11 @@ public String toString() {
17161713

17171714
/**
17181715
* Make a pretty-printed JSON text of this JSONArray.
1719-
*
1716+
*
17201717
* <p>If <pre> {@code indentFactor > 0}</pre> and the {@link JSONArray} has only
17211718
* one element, then the array will be output on a single line:
17221719
* <pre>{@code [1]}</pre>
1723-
*
1720+
*
17241721
* <p>If an array has 2 or more elements, then it will be output across
17251722
* multiple lines: <pre>{@code
17261723
* [
@@ -1732,8 +1729,9 @@ public String toString() {
17321729
* <p><b>
17331730
* Warning: This method assumes that the data structure is acyclical.
17341731
* </b>
1735-
*
1736-
* @param indentFactor The number of spaces to add to each level of indentation.
1732+
*
1733+
* @param indentFactor
1734+
* The number of spaces to add to each level of indentation.
17371735
* @return a printable, displayable, transmittable representation of the
17381736
* object, beginning with <code>[</code>&nbsp;<small>(left
17391737
* bracket)</small> and ending with <code>]</code>
@@ -1742,18 +1740,25 @@ public String toString() {
17421740
*/
17431741
@SuppressWarnings("resource")
17441742
public String toString(int indentFactor) throws JSONException {
1745-
int initialSize = calculateInitialBufferSize(commaMultiplier);
1746-
Writer stringWriter = new StringBuilderWriter(Math.max(initialSize, minimumBufferSize));
1747-
return this.write(stringWriter, indentFactor, 0).toString();
1743+
// each value requires a comma, so multiply the count by 2
1744+
// We don't want to oversize the initial capacity
1745+
int initialSize = myArrayList.size() * commaMultiplier;
1746+
Writer sw = new StringBuilderWriter(Math.max(initialSize, minimumBufferSize));
1747+
return this.write(sw, indentFactor, 0).toString();
17481748
}
17491749

17501750
/**
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
1751+
* Write the contents of the JSONArray as JSON text to a writer. For
1752+
* compactness, no whitespace is added.
1753+
* <p><b>
1754+
* Warning: This method assumes that the data structure is acyclical.
1755+
*</b>
1756+
* @param writer the writer object
1757+
* @return The writer.
1758+
* @throws JSONException if a called function fails
17541759
*/
1755-
private int calculateInitialBufferSize(int elementsPerItem) {
1756-
return myArrayList.size() * elementsPerItem;
1760+
public Writer write(Writer writer) throws JSONException {
1761+
return this.write(writer, 0, 0);
17571762
}
17581763

17591764
/**

0 commit comments

Comments
 (0)