src/main/java/com/github/wnameless/json/base/JsonPrinter.java
public static String prettyPrint(String json, String indentStr) {
    if (json == null) new NullPointerException();
    StringBuilder prettyPrintBuilder = new StringBuilder();

    int indentLevel = 0;
    boolean inQuote = false;
    boolean inBracket = false;
    for (char jsonChar : json.toCharArray()) {
      switch (jsonChar) {
        case '"':
          inQuote = !inQuote;
          prettyPrintBuilder.append(jsonChar);
          break;
        case '{':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            indentLevel++;
            appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
          }
          break;
        case '}':
          if (!inQuote) {
            indentLevel--;
            appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
          }
          prettyPrintBuilder.append(jsonChar);
          break;
        case '[':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            inBracket = true;
            prettyPrintBuilder.append(' ');
          }
          break;
        case ']':
          if (!inQuote) {
            inBracket = false;
            prettyPrintBuilder.append(' ');
          }
          prettyPrintBuilder.append(jsonChar);
          break;
        case ',':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            if (inBracket) {
              prettyPrintBuilder.append(' ');
            } else {
              appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
            }
          }
          break;
        case ':':
          if (inQuote) {
            prettyPrintBuilder.append(jsonChar);
          } else {
            prettyPrintBuilder.append(' ');
            prettyPrintBuilder.append(jsonChar);
            prettyPrintBuilder.append(' ');
          }
          break;
        default:
          if (inQuote || !Character.toString(jsonChar).matches("\\s")) {
            prettyPrintBuilder.append(jsonChar);
          }
      }
    }

    return prettyPrintBuilder.toString();
  }
/**
   * Returns a pretty print JSON of any JSON input.
   * 
   * @param json
   *          any JSON
   * @param indentStr
   *          a string to use as an indent
   * @return a pretty print JSON
   */
Returns a pretty print JSON of any JSON input.

@param json
any JSON
@param indentStr
a string to use as an indent
@return a pretty print JSON
Returns a pretty print JSON of any JSON input . @param json any JSON @param indentStr a string to use as an indent @return a pretty print JSON
public static String prettyPrint(String json, String indentStr) {
    if (json == null) new NullPointerException();
    StringBuilder prettyPrintBuilder = new StringBuilder();
    int indentLevel = 0;
    boolean inQuote = false;
    boolean inBracket = false;
    for (char jsonChar : json.toCharArray()) {
      switch (jsonChar) {
        case '"':
          inQuote = !inQuote;
          prettyPrintBuilder.append(jsonChar);
          break;
        case '{':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            indentLevel++;
            appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
          }
          break;
        case '}':
          if (!inQuote) {
            indentLevel--;
            appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
          }
          prettyPrintBuilder.append(jsonChar);
          break;
        case '[':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            inBracket = true;
            prettyPrintBuilder.append(' ');
          }
          break;
        case ']':
          if (!inQuote) {
            inBracket = false;
            prettyPrintBuilder.append(' ');
          }
          prettyPrintBuilder.append(jsonChar);
          break;
        case ',':
          prettyPrintBuilder.append(jsonChar);
          if (!inQuote) {
            if (inBracket) {
              prettyPrintBuilder.append(' ');
            } else {
              appendNewLine(prettyPrintBuilder, indentLevel, indentStr);
            }
          }
          break;
        case ':':
          if (inQuote) {
            prettyPrintBuilder.append(jsonChar);
          } else {
            prettyPrintBuilder.append(' ');
            prettyPrintBuilder.append(jsonChar);
            prettyPrintBuilder.append(' ');
          }
          break;
        default:
          if (inQuote || !Character.toString(jsonChar).matches("\\s")) {
            prettyPrintBuilder.append(jsonChar);
          }
      }
    }
    return prettyPrintBuilder.toString();
  }
public static String prettyPrint ( String json , String indentStr ) { if ( json == null ) new NullPointerException ( ) ; StringBuilder prettyPrintBuilder = new StringBuilder ( ) ; int indentLevel = 0 ; boolean inQuote = false ; boolean inBracket = false ; for ( char jsonChar : json . toCharArray ( ) ) { switch ( jsonChar ) { case '"' : inQuote = ! inQuote ; prettyPrintBuilder . append ( jsonChar ) ; break ; case '{' : prettyPrintBuilder . append ( jsonChar ) ; if ( ! inQuote ) { indentLevel ++ ; appendNewLine ( prettyPrintBuilder , indentLevel , indentStr ) ; } break ; case '}' : if ( ! inQuote ) { indentLevel -- ; appendNewLine ( prettyPrintBuilder , indentLevel , indentStr ) ; } prettyPrintBuilder . append ( jsonChar ) ; break ; case '[' : prettyPrintBuilder . append ( jsonChar ) ; if ( ! inQuote ) { inBracket = true ; prettyPrintBuilder . append ( ' ' ) ; } break ; case ']' : if ( ! inQuote ) { inBracket = false ; prettyPrintBuilder . append ( ' ' ) ; } prettyPrintBuilder . append ( jsonChar ) ; break ; case ',' : prettyPrintBuilder . append ( jsonChar ) ; if ( ! inQuote ) { if ( inBracket ) { prettyPrintBuilder . append ( ' ' ) ; } else { appendNewLine ( prettyPrintBuilder , indentLevel , indentStr ) ; } } break ; case ':' : if ( inQuote ) { prettyPrintBuilder . append ( jsonChar ) ; } else { prettyPrintBuilder . append ( ' ' ) ; prettyPrintBuilder . append ( jsonChar ) ; prettyPrintBuilder . append ( ' ' ) ; } break ; default : if ( inQuote || ! Character . toString ( jsonChar ) . matches ( "\\s" ) ) { prettyPrintBuilder . append ( jsonChar ) ; } } } return prettyPrintBuilder . toString ( ) ; }
Returns a pretty print JSON of any JSON input.
Returns a pretty print JSON of any JSON input .