Skip to content

Commit 9115ada

Browse files
Performance improvments by Valentin Valchev
1 parent 319e3b9 commit 9115ada

File tree

2 files changed

+219
-270
lines changed

2 files changed

+219
-270
lines changed

JSONArray.java

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
2525
*/
2626

2727
import java.io.IOException;
28+
import java.io.StringWriter;
2829
import java.io.Writer;
2930
import java.lang.reflect.Array;
3031
import java.util.ArrayList;
@@ -60,23 +61,23 @@ of this software and associated documentation files (the "Software"), to deal
6061
* accept:
6162
* <ul>
6263
* <li>An extra <code>,</code>&nbsp;<small>(comma)</small> may appear just
63-
* before the closing bracket.</li>
64-
* <li>The <code>null</code> value will be inserted when there
65-
* is <code>,</code>&nbsp;<small>(comma)</small> elision.</li>
64+
* before the closing bracket.</li>
65+
* <li>The <code>null</code> value will be inserted when there is <code>,</code>
66+
* &nbsp;<small>(comma)</small> elision.</li>
6667
* <li>Strings may be quoted with <code>'</code>&nbsp;<small>(single
67-
* quote)</small>.</li>
68+
* quote)</small>.</li>
6869
* <li>Strings do not need to be quoted at all if they do not begin with a quote
69-
* or single quote, and if they do not contain leading or trailing spaces,
70-
* and if they do not contain any of these characters:
71-
* <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
72-
* and if they are not the reserved words <code>true</code>,
73-
* <code>false</code>, or <code>null</code>.</li>
70+
* or single quote, and if they do not contain leading or trailing spaces, and
71+
* if they do not contain any of these characters:
72+
* <code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers and
73+
* if they are not the reserved words <code>true</code>, <code>false</code>, or
74+
* <code>null</code>.</li>
7475
* <li>Values can be separated by <code>;</code> <small>(semicolon)</small> as
75-
* well as by <code>,</code> <small>(comma)</small>.</li>
76+
* well as by <code>,</code> <small>(comma)</small>.</li>
7677
* </ul>
77-
78+
*
7879
* @author JSON.org
79-
* @version 2011-12-19
80+
* @version 2012-04-20
8081
*/
8182
public class JSONArray {
8283

@@ -555,8 +556,8 @@ public String optString(int index) {
555556
public String optString(int index, String defaultValue) {
556557
Object object = this.opt(index);
557558
return JSONObject.NULL.equals(object)
558-
? defaultValue
559-
: object.toString();
559+
? defaultValue : object
560+
.toString();
560561
}
561562

562563

@@ -834,87 +835,72 @@ public String toString() {
834835
* @throws JSONException
835836
*/
836837
public String toString(int indentFactor) throws JSONException {
837-
return this.toString(indentFactor, 0);
838+
StringWriter sw = new StringWriter();
839+
synchronized (sw.getBuffer()) {
840+
return this.write(sw, indentFactor, 0).toString();
841+
}
838842
}
839843

840-
841844
/**
842-
* Make a prettyprinted JSON text of this JSONArray.
845+
* Write the contents of the JSONArray as JSON text to a writer. For
846+
* compactness, no whitespace is added.
847+
* <p>
843848
* Warning: This method assumes that the data structure is acyclical.
844-
* @param indentFactor The number of spaces to add to each level of
845-
* indentation.
846-
* @param indent The indention of the top level.
847-
* @return a printable, displayable, transmittable
848-
* representation of the array.
849+
*
850+
* @return The writer.
849851
* @throws JSONException
850852
*/
851-
String toString(int indentFactor, int indent) throws JSONException {
852-
int len = this.length();
853-
if (len == 0) {
854-
return "[]";
855-
}
856-
int i;
857-
StringBuffer sb = new StringBuffer("[");
858-
if (len == 1) {
859-
sb.append(JSONObject.valueToString(this.myArrayList.get(0),
860-
indentFactor, indent));
861-
} else {
862-
int newindent = indent + indentFactor;
863-
sb.append('\n');
864-
for (i = 0; i < len; i += 1) {
865-
if (i > 0) {
866-
sb.append(",\n");
867-
}
868-
for (int j = 0; j < newindent; j += 1) {
869-
sb.append(' ');
870-
}
871-
sb.append(JSONObject.valueToString(this.myArrayList.get(i),
872-
indentFactor, newindent));
873-
}
874-
sb.append('\n');
875-
for (i = 0; i < indent; i += 1) {
876-
sb.append(' ');
877-
}
878-
}
879-
sb.append(']');
880-
return sb.toString();
853+
public Writer write(Writer writer) throws JSONException {
854+
return this.write(writer, 0, 0);
881855
}
882856

883-
884857
/**
885-
* Write the contents of the JSONArray as JSON text to a writer.
886-
* For compactness, no whitespace is added.
858+
* Write the contents of the JSONArray as JSON text to a writer. For
859+
* compactness, no whitespace is added.
887860
* <p>
888861
* Warning: This method assumes that the data structure is acyclical.
889862
*
863+
* @param indentFactor
864+
* The number of spaces to add to each level of indentation.
865+
* @param indent
866+
* The indention of the top level.
890867
* @return The writer.
891868
* @throws JSONException
892869
*/
893-
public Writer write(Writer writer) throws JSONException {
870+
Writer write(Writer writer, int indentFactor, int indent)
871+
throws JSONException {
894872
try {
895-
boolean b = false;
896-
int len = this.length();
897-
873+
boolean commanate = false;
874+
int length = this.length();
898875
writer.write('[');
899876

900-
for (int i = 0; i < len; i += 1) {
901-
if (b) {
902-
writer.write(',');
877+
if (length == 1) {
878+
JSONObject.writeValue(writer, this.myArrayList.get(0),
879+
indentFactor, indent);
880+
} else if (length != 0) {
881+
final int newindent = indent + indentFactor;
882+
883+
for (int i = 0; i < length; i += 1) {
884+
if (commanate) {
885+
writer.write(',');
886+
}
887+
if (indentFactor > 0) {
888+
writer.write('\n');
889+
}
890+
JSONObject.indent(writer, newindent);
891+
JSONObject.writeValue(writer, this.myArrayList.get(i),
892+
indentFactor, newindent);
893+
commanate = true;
903894
}
904-
Object v = this.myArrayList.get(i);
905-
if (v instanceof JSONObject) {
906-
((JSONObject)v).write(writer);
907-
} else if (v instanceof JSONArray) {
908-
((JSONArray)v).write(writer);
909-
} else {
910-
writer.write(JSONObject.valueToString(v));
895+
if (indentFactor > 0) {
896+
writer.write('\n');
911897
}
912-
b = true;
898+
JSONObject.indent(writer, indent);
913899
}
914900
writer.write(']');
915901
return writer;
916902
} catch (IOException e) {
917903
throw new JSONException(e);
918904
}
919905
}
920-
}
906+
}

0 commit comments

Comments
 (0)