Logo Search packages:      
Sourcecode: libjson-java version File versions

String net::sf::json::JSONObject::toString ( int  indentFactor,
int  indent 
) [inline]

Make a prettyprinted JSON text of this JSONObject.

Warning: This method assumes that the data structure is acyclical.

Parameters:
indentFactor The number of spaces to add to each level of indentation.
indent The indentation of the top level.
Returns:
a printable, displayable, transmittable representation of the object, beginning with { (left brace) and ending with } (right brace).
Exceptions:
JSONException If the object contains an invalid number.

Implements net::sf::json::JSON.

Definition at line 2615 of file JSONObject.java.

References isNullObject(), keys(), size(), and toString().

                                                          {
      if( isNullObject() ){
         return JSONNull.getInstance()
               .toString();
      }
      int i;
      int n = size();
      if( n == 0 ){
         return "{}";
      }
      if( indentFactor == 0 ){
         return this.toString();
      }
      Iterator keys = keys();
      StringBuffer sb = new StringBuffer( "{" );
      int newindent = indent + indentFactor;
      Object o;
      if( n == 1 ){
         o = keys.next();
         sb.append( JSONUtils.quote( o.toString() ) );
         sb.append( ": " );
         sb.append( JSONUtils.valueToString( this.properties.get( o ), indentFactor, indent ) );
      }else{
         while( keys.hasNext() ){
            o = keys.next();
            if( sb.length() > 1 ){
               sb.append( ",\n" );
            }else{
               sb.append( '\n' );
            }
            for( i = 0; i < newindent; i += 1 ){
               sb.append( ' ' );
            }
            sb.append( JSONUtils.quote( o.toString() ) );
            sb.append( ": " );
            sb.append( JSONUtils.valueToString( this.properties.get( o ), indentFactor, newindent ) );
         }
         if( sb.length() > 1 ){
            sb.append( '\n' );
            for( i = 0; i < indent; i += 1 ){
               sb.append( ' ' );
            }
         }
         for( i = 0; i < indent; i += 1 ){
            sb.insert( 0, ' ' );
         }
      }
      sb.append( '}' );
      return sb.toString();
   }


Generated by  Doxygen 1.6.0   Back to index