32
32
import jakarta .json .stream .JsonParsingException ;
33
33
34
34
import javax .annotation .Nullable ;
35
- import java .io .IOException ;
36
35
import java .io .StringReader ;
37
36
import java .io .Writer ;
38
37
import java .util .AbstractMap ;
@@ -242,15 +241,51 @@ public static void serializeIntOrNull(JsonGenerator generator, int value, int de
242
241
}
243
242
}
244
243
244
+ /**
245
+ * Renders a <code>JsonpSerializable</code> as a string by serializing it to JSON. Any object of an application-specific
246
+ * class in the object graph is rendered using that object's <code>toString()</code> representation as a JSON string value.
247
+ * <p>
248
+ * The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
249
+ *
250
+ * @see #MAX_TO_STRING_LENGTH
251
+ */
245
252
public static String toString (JsonpSerializable value ) {
246
253
return toString (value , ToStringMapper .INSTANCE , new StringBuilder ()).toString ();
247
254
}
248
255
256
+ /**
257
+ * Maximum length of the <code>toString</code> representation of a <code>JsonpSerializable</code>.
258
+ * <p>
259
+ * The default is 10k characters, and can be changed globally by changing the value of this field.
260
+ */
261
+ public static int MAX_TO_STRING_LENGTH = 10000 ;
262
+
263
+ private static class ToStringTooLongException extends RuntimeException {
264
+ }
265
+
266
+ /**
267
+ * Renders a <code>JsonpSerializable</code> as a string in a destination <code>StringBuilder</code>by serializing it to JSON.
268
+ * <p>
269
+ * The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
270
+ *
271
+ * @return the <code>dest</code> parameter, for chaining.
272
+ * @see #toString(JsonpSerializable)
273
+ * @see #MAX_TO_STRING_LENGTH
274
+ */
249
275
public static StringBuilder toString (JsonpSerializable value , JsonpMapper mapper , StringBuilder dest ) {
250
276
Writer writer = new Writer () {
277
+ int length = 0 ;
251
278
@ Override
252
- public void write (char [] cbuf , int off , int len ) throws IOException {
253
- dest .append (cbuf , off , len );
279
+ public void write (char [] cbuf , int off , int len ) {
280
+ int max = MAX_TO_STRING_LENGTH ;
281
+ length += len ;
282
+ if (length > max ) {
283
+ dest .append (cbuf , off , len - (length - max ));
284
+ dest .append ("..." );
285
+ throw new ToStringTooLongException ();
286
+ } else {
287
+ dest .append (cbuf , off , len );
288
+ }
254
289
}
255
290
256
291
@ Override
@@ -263,11 +298,26 @@ public void close() {
263
298
};
264
299
265
300
JsonGenerator generator = mapper .jsonProvider ().createGenerator (writer );
266
- value .serialize (generator , mapper );
267
- generator .close ();
301
+ try {
302
+ value .serialize (generator , mapper );
303
+ generator .close ();
304
+ } catch (ToStringTooLongException e ) {
305
+ // Ignore
306
+ }
268
307
return dest ;
269
308
}
270
309
310
+ /**
311
+ * Renders a <code>JsonpSerializable</code> as a string in a destination <code>StringBuilder</code>by serializing it to JSON.
312
+ * Any object of an application-specific class in the object graph is rendered using that object's <code>toString()</code>
313
+ * representation as a JSON string value.
314
+ * <p>
315
+ * The size of the string is limited to {@link #MAX_TO_STRING_LENGTH}.
316
+ *
317
+ * @return the <code>dest</code> parameter, for chaining.
318
+ * @see #toString(JsonpSerializable)
319
+ * @see #MAX_TO_STRING_LENGTH
320
+ */
271
321
public static StringBuilder toString (JsonpSerializable value , StringBuilder dest ) {
272
322
return toString (value , ToStringMapper .INSTANCE , dest );
273
323
}
0 commit comments