Skip to content
Closed
28 changes: 12 additions & 16 deletions JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ of this software and associated documentation files (the "Software"), to deal
*/

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down Expand Up @@ -78,7 +76,7 @@ of this software and associated documentation files (the "Software"), to deal
* </ul>
*
* @author JSON.org
* @version 2016-07-19
* @version 2016-08-04
*/
public class JSONArray implements Iterable<Object> {

Expand Down Expand Up @@ -447,7 +445,7 @@ public String join(String separator) throws JSONException {
if (i > 0) {
sb.append(separator);
}
sb.append(JSONObject.valueToString(this.myArrayList.get(i)));
JSONObject.writeValue(sb, this.myArrayList.get(i));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Write directly to the one StringBuilder, avoiding creation of temporary buffer.

}
return sb.toString();
}
Expand Down Expand Up @@ -1017,7 +1015,7 @@ public JSONArray put(int index, Object value) throws JSONException {
}

/**
* Creates a JSONPointer using an initialization string and tries to
* Creates a JSONPointer using an initialization string and tries to
* match it to an item within this JSONArray. For example, given a
* JSONArray initialized with this document:
* <pre>
Expand Down Expand Up @@ -1159,10 +1157,8 @@ public String toString() {
* @throws JSONException
*/
public String toString(int indentFactor) throws JSONException {
StringWriter sw = new StringWriter();
synchronized (sw.getBuffer()) {
return this.write(sw, indentFactor, 0).toString();
}
StringBuilder sw = new StringBuilder();
return this.write(sw, indentFactor, 0).toString();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reverts a change related to #54

Basically, now that we use StringBuilder, which is not synchronizing on a buffer, instead of StringWriter, which does, the rationale for synchronizing no longer applies.

/**
Expand All @@ -1174,7 +1170,7 @@ public String toString(int indentFactor) throws JSONException {
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer) throws JSONException {
public <T extends Appendable> T write(T writer) throws JSONException {
return this.write(writer, 0, 0);
}

Expand All @@ -1193,12 +1189,12 @@ public Writer write(Writer writer) throws JSONException {
* @return The writer.
* @throws JSONException
*/
public Writer write(Writer writer, int indentFactor, int indent)
public <T extends Appendable> T write(T writer, int indentFactor, int indent)
throws JSONException {
try {
boolean commanate = false;
int length = this.length();
writer.write('[');
writer.append('[');

if (length == 1) {
JSONObject.writeValue(writer, this.myArrayList.get(0),
Expand All @@ -1208,22 +1204,22 @@ public Writer write(Writer writer, int indentFactor, int indent)

for (int i = 0; i < length; i += 1) {
if (commanate) {
writer.write(',');
writer.append(',');
}
if (indentFactor > 0) {
writer.write('\n');
writer.append('\n');
}
JSONObject.indent(writer, newindent);
JSONObject.writeValue(writer, this.myArrayList.get(i),
indentFactor, newindent);
commanate = true;
}
if (indentFactor > 0) {
writer.write('\n');
writer.append('\n');
}
JSONObject.indent(writer, indent);
}
writer.write(']');
writer.append(']');
return writer;
} catch (IOException e) {
throw new JSONException(e);
Expand Down
Loading