Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9115ada

Browse files
Performance improvments by Valentin Valchev
1 parent319e3b9 commit9115ada

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
importjava.io.IOException;
28+
importjava.io.StringWriter;
2829
importjava.io.Writer;
2930
importjava.lang.reflect.Array;
3031
importjava.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-
* andif they do not contain any of these characters:
71-
*<code>{ } [ ] / \ : , = ; #</code> and if they do not look like numbers
72-
* andif 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-
* @version2011-12-19
80+
* @version2012-04-20
8081
*/
8182
publicclassJSONArray {
8283

@@ -555,8 +556,8 @@ public String optString(int index) {
555556
publicStringoptString(intindex,StringdefaultValue) {
556557
Objectobject =this.opt(index);
557558
returnJSONObject.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
publicStringtoString(intindentFactor)throwsJSONException {
837-
returnthis.toString(indentFactor,0);
838+
StringWritersw =newStringWriter();
839+
synchronized (sw.getBuffer()) {
840+
returnthis.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-
StringtoString(intindentFactor,intindent)throwsJSONException {
852-
intlen =this.length();
853-
if (len ==0) {
854-
return"[]";
855-
}
856-
inti;
857-
StringBuffersb =newStringBuffer("[");
858-
if (len ==1) {
859-
sb.append(JSONObject.valueToString(this.myArrayList.get(0),
860-
indentFactor,indent));
861-
}else {
862-
intnewindent =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 (intj =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-
returnsb.toString();
853+
publicWriterwrite(Writerwriter)throwsJSONException {
854+
returnthis.write(writer,0,0);
881855
}
882856

883-
884857
/**
885-
* Write the contents of the JSONArray as JSON text to a writer.
886-
*Forcompactness, 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-
publicWriterwrite(Writerwriter)throwsJSONException {
870+
Writerwrite(Writerwriter,intindentFactor,intindent)
871+
throwsJSONException {
894872
try {
895-
booleanb =false;
896-
intlen =this.length();
897-
873+
booleancommanate =false;
874+
intlength =this.length();
898875
writer.write('[');
899876

900-
for (inti =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+
}elseif (length !=0) {
881+
finalintnewindent =indent +indentFactor;
882+
883+
for (inti =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-
Objectv =this.myArrayList.get(i);
905-
if (vinstanceofJSONObject) {
906-
((JSONObject)v).write(writer);
907-
}elseif (vinstanceofJSONArray) {
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
returnwriter;
916902
}catch (IOExceptione) {
917903
thrownewJSONException(e);
918904
}
919905
}
920-
}
906+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp