@@ -17,77 +17,41 @@ public final class ProblemDetailRenderer {
1717 /** Initial capacity of the JSON buffer; sized for a typical problem-detail document. */
1818 private static final int INITIAL_BUFFER_CAPACITY = 128 ;
1919
20- /** Codepoints below this value are control characters and must be unicode-escaped in JSON. */
21- private static final int FIRST_PRINTABLE_ASCII = 0x20 ;
22-
2320 private ProblemDetailRenderer () {}
2421
2522 public static String render (int status , String title , String detail ) {
2623 StringBuilder out = new StringBuilder (INITIAL_BUFFER_CAPACITY );
2724 out .append ('{' );
28- appendStringField (out , "type" , PROBLEM_TYPE );
25+ JsonStrings . appendStringField (out , "type" , PROBLEM_TYPE );
2926 out .append (',' );
30- appendStringField (out , "title" , title );
27+ JsonStrings . appendStringField (out , "title" , title );
3128 out .append (',' );
3229 appendIntField (out , "status" , status );
3330 out .append (',' );
34- appendStringField (out , "detail" , detail );
31+ JsonStrings . appendStringField (out , "detail" , detail );
3532 out .append ('}' );
3633 return out .toString ();
3734 }
3835
3936 public static String render (ValidationError error ) {
4037 StringBuilder out = new StringBuilder (INITIAL_BUFFER_CAPACITY );
4138 out .append ('{' );
42- appendStringField (out , "type" , PROBLEM_TYPE );
39+ JsonStrings . appendStringField (out , "type" , PROBLEM_TYPE );
4340 out .append (',' );
44- appendStringField (out , "title" , PROBLEM_TITLE );
41+ JsonStrings . appendStringField (out , "title" , PROBLEM_TITLE );
4542 out .append (',' );
4643 appendIntField (out , "status" , PROBLEM_STATUS );
4744 out .append (',' );
48- appendStringField (out , "detail" , error .message ());
45+ JsonStrings . appendStringField (out , "detail" , error .message ());
4946 out .append (',' );
50- appendStringField (out , "pointer" , error .pointer ());
47+ JsonStrings . appendStringField (out , "pointer" , error .pointer ());
5148 out .append (',' );
52- appendStringField (out , "keyword" , error .keyword ());
49+ JsonStrings . appendStringField (out , "keyword" , error .keyword ());
5350 out .append ('}' );
5451 return out .toString ();
5552 }
5653
57- private static void appendStringField (StringBuilder out , String name , String value ) {
58- out .append ('"' ).append (name ).append ("\" :\" " );
59- appendEscaped (out , value );
60- out .append ('"' );
61- }
62-
6354 private static void appendIntField (StringBuilder out , String name , int value ) {
6455 out .append ('"' ).append (name ).append ("\" :" ).append (value );
6556 }
66-
67- /**
68- * Appends {@code value} to {@code out} with JSON-string escaping applied. Handles the six
69- * mandatory escape sequences and emits {@code \uXXXX} for control characters below {@link
70- * #FIRST_PRINTABLE_ASCII}.
71- */
72- private static void appendEscaped (StringBuilder out , String value ) {
73- for (int i = 0 ; i < value .length (); i ++) {
74- char c = value .charAt (i );
75- switch (c ) {
76- case '\\' -> out .append ("\\ \\ " );
77- case '"' -> out .append ("\\ \" " );
78- case '\n' -> out .append ("\\ n" );
79- case '\r' -> out .append ("\\ r" );
80- case '\t' -> out .append ("\\ t" );
81- default -> appendUnicodeOrLiteral (out , c );
82- }
83- }
84- }
85-
86- private static void appendUnicodeOrLiteral (StringBuilder out , char c ) {
87- if (c < FIRST_PRINTABLE_ASCII ) {
88- out .append (String .format ("\\ u%04x" , (int ) c ));
89- } else {
90- out .append (c );
91- }
92- }
9357}
0 commit comments