@@ -459,6 +459,11 @@ public static String objectString(String name, Object o) {
459
459
}
460
460
461
461
private static final int INDENT_WIDTH = 4 ;
462
+ private static final String INDENT = " " ;
463
+ private static String indent (int level ) {
464
+ return level == 0 ? "" : INDENT .substring (0 , level * INDENT_WIDTH );
465
+ }
466
+
462
467
public static String getFormatted (Object o ) {
463
468
String s = o .toString ();
464
469
String newline = System .lineSeparator ();
@@ -467,14 +472,14 @@ public static String getFormatted(Object o) {
467
472
boolean begin_quotes = false ;
468
473
469
474
boolean opened = false ;
470
- int indent = 0 ;
475
+ int indentLevel = 0 ;
476
+ String indent = "" ;
471
477
for (int x = 0 ; x < s .length (); x ++) {
472
478
char c = s .charAt (x );
473
479
474
480
if (c == '\"' ) {
475
481
if (opened ) {
476
- sb .append (newline )
477
- .append (String .format ("%" + (indent += INDENT_WIDTH ) + "s" , "" ));
482
+ sb .append (newline ).append (indent );
478
483
opened = false ;
479
484
}
480
485
sb .append (c );
@@ -488,12 +493,13 @@ public static String getFormatted(Object o) {
488
493
case '[' :
489
494
sb .append (c );
490
495
opened = true ;
496
+ indent = indent (++indentLevel );
491
497
continue ;
492
498
case '}' :
493
499
case ']' :
500
+ indent = indent (--indentLevel );
494
501
if (!opened ) {
495
- sb .append (newline )
496
- .append ((indent -= INDENT_WIDTH ) > 0 ? String .format ("%" + indent + "s" , "" ) : "" );
502
+ sb .append (newline ).append (indent );
497
503
}
498
504
sb .append (c );
499
505
opened = false ;
@@ -502,13 +508,12 @@ public static String getFormatted(Object o) {
502
508
sb .append (c ).append (" " );
503
509
continue ;
504
510
case ',' :
505
- sb .append (c ).append (newline ).append (indent > 0 ? String . format ( "%" + indent + "s" , "" ) : "" );
511
+ sb .append (c ).append (newline ).append (indentLevel > 0 ? indent : "" );
506
512
continue ;
507
513
default :
508
514
if (Character .isWhitespace (c )) continue ;
509
515
if (opened ) {
510
- sb .append (newline )
511
- .append (String .format ("%" + (indent += INDENT_WIDTH ) + "s" , "" ));
516
+ sb .append (newline ).append (indent );
512
517
opened = false ;
513
518
}
514
519
}
0 commit comments