public Writer write ( Writer writer , int indentFactor , int indent ) throws JSONException { try { boolean commanate = false ; final int length = this . length ( ) ; writer . write ( ' ' ) ; if ( length == 1 ) { final Entry < String , ? > entry = this . entrySet ( ) . iterator ( ) . next ( ) ; final String key = entry . getKey ( ) ; writer . write ( quote ( key ) ) ; writer . write ( ' ' ) ; if ( indentFactor > 0 ) { writer . write ( ' ' ) ; } try { writeValue ( writer , entry . getValue ( ) , indentFactor , indent ) ; } catch ( Exception e ) { throw new JSONException ( "Unable to write JSONObject value for key: " + key , e ) ; } } else if ( length != 0 ) { final int newindent = indent + indentFactor ; for ( final Entry < String , ? > entry : this . entrySet ( ) ) { if ( commanate ) { writer . write ( ' ' ) ; } if ( indentFactor > 0 ) { writer . write ( ' ' ) ; } indent ( writer , newindent ) ; final String key = entry . getKey ( ) ; writer . write ( quote ( key ) ) ; writer . write ( ' ' ) ; if ( indentFactor > 0 ) { writer . write ( ' ' ) ; } try { writeValue ( writer , entry . getValue ( ) , indentFactor , newindent ) ; } catch ( Exception e ) { throw new JSONException ( "Unable to write JSONObject value for key: " + key , e ) ; } commanate = true ; } if ( indentFactor > 0 ) { writer . write ( ' ' ) ; } indent ( writer , indent ) ; } writer . write ( ' ' ) ; return writer ; } catch ( IOException exception ) { throw new JSONException ( exception ) ; } }
Write the contents of the JSONObject as JSON text to a writer .