@@ -458,63 +458,65 @@ public static String objectString(String name, Object o) {
458
458
return o .toString ();
459
459
}
460
460
461
- private static final String INDENT = " " ;
462
- private static String indent (int level ) {
463
- return level == 0 ? "" : INDENT .substring (0 , level * 4 );
464
- }
465
-
466
- /**
467
- * This isn't perfect but good enough for debugging
468
- * @param o the object
469
- * @return the formatted string
470
- */
461
+ private static final int INDENT_WIDTH = 4 ;
471
462
public static String getFormatted (Object o ) {
472
- StringBuilder sb = new StringBuilder ();
473
- int level = 0 ;
474
- int arrayLevel = 0 ;
475
- boolean lastWasClose = false ;
476
- boolean indentNext = true ;
477
- String indent = "" ;
478
463
String s = o .toString ();
464
+ String newline = System .lineSeparator ();
465
+
466
+ StringBuilder sb = new StringBuilder ();
467
+ boolean begin_quotes = false ;
468
+
469
+ boolean opened = false ;
470
+ int indent = 0 ;
479
471
for (int x = 0 ; x < s .length (); x ++) {
480
472
char c = s .charAt (x );
481
- if (c == '{' ) {
482
- if (arrayLevel > 0 && lastWasClose ) {
483
- sb .append (indent );
473
+
474
+ if (c == '\"' ) {
475
+ if (opened ) {
476
+ sb .append (newline )
477
+ .append (String .format ("%" + (indent += INDENT_WIDTH ) + "s" , "" ));
478
+ opened = false ;
484
479
}
485
- sb .append (c ).append ('\n' );
486
- indent = indent (++level );
487
- indentNext = true ;
488
- lastWasClose = false ;
480
+ sb .append (c );
481
+ begin_quotes = !begin_quotes ;
482
+ continue ;
489
483
}
490
- else if (c == '}' ) {
491
- indent = indent (--level );
492
- sb .append ('\n' ).append (indent ).append (c );
493
- lastWasClose = true ;
494
- }
495
- else if (c == ',' ) {
496
- sb .append (",\n " );
497
- indentNext = true ;
498
- }
499
- else {
500
- if (c == '[' ) {
501
- arrayLevel ++;
502
- }
503
- else if (c == ']' ) {
504
- arrayLevel --;
505
- }
506
- if (indentNext ) {
507
- if (c != ' ' ) {
508
- sb .append (indent ).append (c );
509
- indentNext = false ;
510
- }
511
- }
512
- else {
513
- sb .append (c );
484
+
485
+ if (!begin_quotes ) {
486
+ switch (c ) {
487
+ case '{' :
488
+ case '[' :
489
+ sb .append (c );
490
+ opened = true ;
491
+ continue ;
492
+ case '}' :
493
+ case ']' :
494
+ if (!opened ) {
495
+ sb .append (newline )
496
+ .append ((indent -= INDENT_WIDTH ) > 0 ? String .format ("%" + indent + "s" , "" ) : "" );
497
+ }
498
+ sb .append (c );
499
+ opened = false ;
500
+ continue ;
501
+ case ':' :
502
+ sb .append (c ).append (" " );
503
+ continue ;
504
+ case ',' :
505
+ sb .append (c ).append (newline ).append (indent > 0 ? String .format ("%" + indent + "s" , "" ) : "" );
506
+ continue ;
507
+ default :
508
+ if (Character .isWhitespace (c )) continue ;
509
+ if (opened ) {
510
+ sb .append (newline )
511
+ .append (String .format ("%" + (indent += INDENT_WIDTH ) + "s" , "" ));
512
+ opened = false ;
513
+ }
514
514
}
515
- lastWasClose = lastWasClose && Character .isWhitespace (c );
516
515
}
516
+
517
+ sb .append (c ).append (c == '\\' ? "" + s .charAt (++x ) : "" );
517
518
}
519
+
518
520
return sb .toString ();
519
521
}
520
522
0 commit comments