22
22
import com .fasterxml .jackson .core .util .BufferRecycler ;
23
23
import com .fasterxml .jackson .core .util .BufferRecyclers ;
24
24
import com .fasterxml .jackson .core .util .JacksonFeature ;
25
+ import com .fasterxml .jackson .core .util .JsonGeneratorDecorator ;
25
26
import com .fasterxml .jackson .core .util .Separators ;
26
27
27
28
/**
@@ -293,6 +294,14 @@ public static int collectDefaults() {
293
294
*/
294
295
protected OutputDecorator _outputDecorator ;
295
296
297
+ /**
298
+ * List of {@link JsonGeneratorDecorator}s to apply to {@link JsonGenerator}s
299
+ * after construction; applied in the order of addition.
300
+ *
301
+ * @since 2.16
302
+ */
303
+ protected final List <JsonGeneratorDecorator > _generatorDecorators ;
304
+
296
305
/**
297
306
* Separator used between root-level values, if any; null indicates
298
307
* "do not add separator".
@@ -319,9 +328,6 @@ public static int collectDefaults() {
319
328
*/
320
329
protected final char _quoteChar ;
321
330
322
- /** @since 2.16 */
323
- protected List <JsonGeneratorDecorator > _generatorDecorators = new ArrayList <>();
324
-
325
331
/*
326
332
/**********************************************************
327
333
/* Construction
@@ -344,6 +350,7 @@ public JsonFactory(ObjectCodec oc) {
344
350
_objectCodec = oc ;
345
351
_quoteChar = DEFAULT_QUOTE_CHAR ;
346
352
_streamReadConstraints = StreamReadConstraints .defaults ();
353
+ _generatorDecorators = null ;
347
354
}
348
355
349
356
/**
@@ -364,6 +371,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
364
371
_generatorFeatures = src ._generatorFeatures ;
365
372
_inputDecorator = src ._inputDecorator ;
366
373
_outputDecorator = src ._outputDecorator ;
374
+ _generatorDecorators = _copy (src ._generatorDecorators );
367
375
_streamReadConstraints = src ._streamReadConstraints == null ?
368
376
StreamReadConstraints .defaults () : src ._streamReadConstraints ;
369
377
@@ -390,6 +398,7 @@ public JsonFactory(JsonFactoryBuilder b) {
390
398
_generatorFeatures = b ._streamWriteFeatures ;
391
399
_inputDecorator = b ._inputDecorator ;
392
400
_outputDecorator = b ._outputDecorator ;
401
+ _generatorDecorators = _copy (b ._generatorDecorators );
393
402
_streamReadConstraints = b ._streamReadConstraints == null ?
394
403
StreamReadConstraints .defaults () : b ._streamReadConstraints ;
395
404
@@ -398,7 +407,6 @@ public JsonFactory(JsonFactoryBuilder b) {
398
407
_rootValueSeparator = b ._rootValueSeparator ;
399
408
_maximumNonEscapedChar = b ._maximumNonEscapedChar ;
400
409
_quoteChar = b ._quoteChar ;
401
- _generatorDecorators = new ArrayList <>(b ._generatorDecorators );
402
410
}
403
411
404
412
/**
@@ -417,6 +425,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
417
425
_generatorFeatures = b ._streamWriteFeatures ;
418
426
_inputDecorator = b ._inputDecorator ;
419
427
_outputDecorator = b ._outputDecorator ;
428
+ _generatorDecorators = _copy (b ._generatorDecorators );
420
429
_streamReadConstraints = b ._streamReadConstraints == null ?
421
430
StreamReadConstraints .defaults () : b ._streamReadConstraints ;
422
431
@@ -487,6 +496,14 @@ protected void _checkInvalidCopy(Class<?> exp)
487
496
}
488
497
}
489
498
499
+ // @since 2.16
500
+ protected static <T > List <T > _copy (List <T > src ) {
501
+ if (src == null ) {
502
+ return src ;
503
+ }
504
+ return new ArrayList <T >(src );
505
+ }
506
+
490
507
/*
491
508
/**********************************************************
492
509
/* Serializable overrides
@@ -1901,13 +1918,6 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
1901
1918
return _decorate (gen );
1902
1919
}
1903
1920
1904
- private JsonGenerator _decorate (JsonGenerator result ) {
1905
- for (JsonGeneratorDecorator decorator : _generatorDecorators ) {
1906
- result = decorator .decorate (this , result );
1907
- }
1908
- return result ;
1909
- }
1910
-
1911
1921
/**
1912
1922
* Overridable factory method that actually instantiates generator for
1913
1923
* given {@link OutputStream} and context object, using UTF-8 encoding.
@@ -2008,6 +2018,25 @@ protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException
2008
2018
return out ;
2009
2019
}
2010
2020
2021
+ /**
2022
+ * Helper method for applying all registered {@link JsonGeneratorDecorator}s
2023
+ * on freshly constructed {@link JsonGenerator}.
2024
+ *
2025
+ * @param g Generator constructed that is to be decorated
2026
+ *
2027
+ * @return Generator after applying all registered {@link JsonGeneratorDecorator}s.
2028
+ *
2029
+ * @since 2.16
2030
+ */
2031
+ protected JsonGenerator _decorate (JsonGenerator g ) {
2032
+ if (_generatorDecorators != null ) {
2033
+ for (JsonGeneratorDecorator decorator : _generatorDecorators ) {
2034
+ g = decorator .decorate (this , g );
2035
+ }
2036
+ }
2037
+ return g ;
2038
+ }
2039
+
2011
2040
/*
2012
2041
/**********************************************************
2013
2042
/* Internal factory methods, other
0 commit comments