@@ -2607,7 +2607,7 @@ public JSONArray toJSONArray(JSONArray names) throws JSONException {
26072607 @ Override
26082608 public String toString () {
26092609 try {
2610- return JSONStringer .format (this , 0 );
2610+ return JSONWriter .format (this , 0 );
26112611 } catch (Exception e ) {
26122612 return null ;
26132613 }
@@ -2641,39 +2641,7 @@ public String toString() {
26412641 */
26422642 @ SuppressWarnings ("resource" )
26432643 public String toString (int indentFactor ) throws JSONException {
2644- return JSONStringer .format (this , indentFactor );
2645- }
2646-
2647- /**
2648- * Make a JSON text of an Object value. If the object has an
2649- * value.toJSONString() method, then that method will be used to produce the
2650- * JSON text. The method is required to produce a strictly conforming text.
2651- * If the object does not contain a toJSONString method (which is the most
2652- * common case), then a text will be produced by other means. If the value
2653- * is an array or Collection, then a JSONArray will be made from it and its
2654- * toJSONString method will be called. If the value is a MAP, then a
2655- * JSONObject will be made from it and its toJSONString method will be
2656- * called. Otherwise, the value's toString method will be called, and the
2657- * result will be quoted.
2658- *
2659- * <p>
2660- * Warning: This method assumes that the data structure is acyclical.
2661- *
2662- * @param value
2663- * The value to be serialized.
2664- * @return a printable, displayable, transmittable representation of the
2665- * object, beginning with <code>{</code> <small>(left
2666- * brace)</small> and ending with <code>}</code> <small>(right
2667- * brace)</small>.
2668- * @throws JSONException
2669- * If the value is or contains an invalid number.
2670- */
2671- public static String valueToString (Object value ) throws JSONException {
2672- // moves the implementation to JSONWriter as:
2673- // 1. It makes more sense to be part of the writer class
2674- // 2. For Android support this method is not available. By implementing it in the Writer
2675- // Android users can use the writer with the built in Android JSONObject implementation.
2676- return JSONWriter .valueToString (value );
2644+ return JSONWriter .format (this , indentFactor );
26772645 }
26782646
26792647 /**
@@ -2774,63 +2742,7 @@ private static Object wrap(Object object, Set<Object> objectsRecord, int recursi
27742742 * @throws JSONException if a called function has an error
27752743 */
27762744 public Writer write (Writer writer ) throws JSONException {
2777- return this .write (writer , 0 , 0 );
2778- }
2779-
2780- @ SuppressWarnings ("resource" )
2781- static final Writer writeValue (Writer writer , Object value ,
2782- int indentFactor , int indent ) throws JSONException , IOException {
2783- if (value == null || value .equals (null )) {
2784- writer .write ("null" );
2785- } else if (value instanceof JSONString ) {
2786- // JSONString must be checked first, so it can overwrite behaviour of other types below
2787- Object o ;
2788- try {
2789- o = ((JSONString ) value ).toJSONString ();
2790- } catch (Exception e ) {
2791- throw new JSONException (e );
2792- }
2793- writer .write (o != null ? o .toString () : quote (value .toString ()));
2794- } else if (value instanceof String ) {
2795- // assuming most values are Strings, so testing it early
2796- quote (value .toString (), writer );
2797- return writer ;
2798- } else if (value instanceof Number ) {
2799- // not all Numbers may match actual JSON Numbers. i.e. fractions or Imaginary
2800- final String numberAsString = numberToString ((Number ) value );
2801- if (NUMBER_PATTERN .matcher (numberAsString ).matches ()) {
2802- writer .write (numberAsString );
2803- } else {
2804- // The Number value is not a valid JSON number.
2805- // Instead we will quote it as a string
2806- quote (numberAsString , writer );
2807- }
2808- } else if (value instanceof Boolean ) {
2809- writer .write (value .toString ());
2810- } else if (value instanceof Enum <?>) {
2811- writer .write (quote (((Enum <?>)value ).name ()));
2812- } else if (value instanceof JSONObject ) {
2813- ((JSONObject ) value ).write (writer , indentFactor , indent );
2814- } else if (value instanceof JSONArray ) {
2815- ((JSONArray ) value ).write (writer , indentFactor , indent );
2816- } else if (value instanceof Map ) {
2817- Map <?, ?> map = (Map <?, ?>) value ;
2818- new JSONObject (map ).write (writer , indentFactor , indent );
2819- } else if (value instanceof Collection ) {
2820- Collection <?> coll = (Collection <?>) value ;
2821- new JSONArray (coll ).write (writer , indentFactor , indent );
2822- } else if (value .getClass ().isArray ()) {
2823- new JSONArray (value ).write (writer , indentFactor , indent );
2824- } else {
2825- quote (value .toString (), writer );
2826- }
2827- return writer ;
2828- }
2829-
2830- static final void indent (Writer writer , int indent ) throws IOException {
2831- for (int i = 0 ; i < indent ; i += 1 ) {
2832- writer .write (' ' );
2833- }
2745+ return JSONWriter .format (this , writer , 0 , 0 );
28342746 }
28352747
28362748 /**
@@ -2861,59 +2773,8 @@ static final void indent(Writer writer, int indent) throws IOException {
28612773 * occurs
28622774 */
28632775 @ SuppressWarnings ("resource" )
2864- public Writer write (Writer writer , int indentFactor , int indent )
2865- throws JSONException {
2866- try {
2867- boolean needsComma = false ;
2868- final int length = this .length ();
2869- writer .write ('{' );
2870-
2871- if (length == 1 ) {
2872- final Entry <String ,?> entry = this .entrySet ().iterator ().next ();
2873- final String key = entry .getKey ();
2874- writer .write (quote (key ));
2875- writer .write (':' );
2876- if (indentFactor > 0 ) {
2877- writer .write (' ' );
2878- }
2879- try {
2880- writeValue (writer , entry .getValue (), indentFactor , indent );
2881- } catch (Exception e ) {
2882- throw new JSONException ("Unable to write JSONObject value for key: " + key , e );
2883- }
2884- } else if (length != 0 ) {
2885- final int newIndent = indent + indentFactor ;
2886- for (final Entry <String ,?> entry : this .entrySet ()) {
2887- if (needsComma ) {
2888- writer .write (',' );
2889- }
2890- if (indentFactor > 0 ) {
2891- writer .write ('\n' );
2892- }
2893- indent (writer , newIndent );
2894- final String key = entry .getKey ();
2895- writer .write (quote (key ));
2896- writer .write (':' );
2897- if (indentFactor > 0 ) {
2898- writer .write (' ' );
2899- }
2900- try {
2901- writeValue (writer , entry .getValue (), indentFactor , newIndent );
2902- } catch (Exception e ) {
2903- throw new JSONException ("Unable to write JSONObject value for key: " + key , e );
2904- }
2905- needsComma = true ;
2906- }
2907- if (indentFactor > 0 ) {
2908- writer .write ('\n' );
2909- }
2910- indent (writer , indent );
2911- }
2912- writer .write ('}' );
2913- return writer ;
2914- } catch (IOException exception ) {
2915- throw new JSONException (exception );
2916- }
2776+ public Writer write (Writer writer , int indentFactor , int indent ) throws JSONException {
2777+ return JSONWriter .format (this , writer , indentFactor , indent );
29172778 }
29182779
29192780 /**
0 commit comments