@@ -25,6 +25,7 @@ of this software and associated documentation files (the "Software"), to deal
25
25
*/
26
26
27
27
import java .io .IOException ;
28
+ import java .io .StringWriter ;
28
29
import java .io .Writer ;
29
30
import java .lang .reflect .Array ;
30
31
import java .util .ArrayList ;
@@ -60,23 +61,23 @@ of this software and associated documentation files (the "Software"), to deal
60
61
* accept:
61
62
* <ul>
62
63
* <li>An extra <code>,</code> <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> <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
+ * <small>(comma)</small> elision.</li>
66
67
* <li>Strings may be quoted with <code>'</code> <small>(single
67
- * quote)</small>.</li>
68
+ * quote)</small>.</li>
68
69
* <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>
74
75
* <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>
76
77
* </ul>
77
-
78
+ *
78
79
* @author JSON.org
79
- * @version2011-12-19
80
+ * @version2012-04-20
80
81
*/
81
82
public class JSONArray {
82
83
@@ -555,8 +556,8 @@ public String optString(int index) {
555
556
public String optString (int index ,String defaultValue ) {
556
557
Object object =this .opt (index );
557
558
return JSONObject .NULL .equals (object )
558
- ?defaultValue
559
- : object .toString ();
559
+ ?defaultValue : object
560
+ .toString ();
560
561
}
561
562
562
563
@@ -834,87 +835,72 @@ public String toString() {
834
835
* @throws JSONException
835
836
*/
836
837
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
+ }
838
842
}
839
843
840
-
841
844
/**
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>
843
848
* 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.
849
851
* @throws JSONException
850
852
*/
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 );
881
855
}
882
856
883
-
884
857
/**
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.
887
860
* <p>
888
861
* Warning: This method assumes that the data structure is acyclical.
889
862
*
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.
890
867
* @return The writer.
891
868
* @throws JSONException
892
869
*/
893
- public Writer write (Writer writer )throws JSONException {
870
+ Writer write (Writer writer ,int indentFactor ,int indent )
871
+ throws JSONException {
894
872
try {
895
- boolean b =false ;
896
- int len =this .length ();
897
-
873
+ boolean commanate =false ;
874
+ int length =this .length ();
898
875
writer .write ('[' );
899
876
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 ;
903
894
}
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' );
911
897
}
912
- b = true ;
898
+ JSONObject . indent ( writer , indent ) ;
913
899
}
914
900
writer .write (']' );
915
901
return writer ;
916
902
}catch (IOException e ) {
917
903
throw new JSONException (e );
918
904
}
919
905
}
920
- }
906
+ }