2222import com .fasterxml .jackson .core .util .BufferRecycler ;
2323import com .fasterxml .jackson .core .util .BufferRecyclers ;
2424import com .fasterxml .jackson .core .util .JacksonFeature ;
25+ import com .fasterxml .jackson .core .util .JsonGeneratorDecorator ;
2526import com .fasterxml .jackson .core .util .Separators ;
2627
2728/**
@@ -293,6 +294,14 @@ public static int collectDefaults() {
293294 */
294295 protected OutputDecorator _outputDecorator ;
295296
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+
296305 /**
297306 * Separator used between root-level values, if any; null indicates
298307 * "do not add separator".
@@ -319,9 +328,6 @@ public static int collectDefaults() {
319328 */
320329 protected final char _quoteChar ;
321330
322- /** @since 2.16 */
323- protected List <JsonGeneratorDecorator > _generatorDecorators = new ArrayList <>();
324-
325331 /*
326332 /**********************************************************
327333 /* Construction
@@ -344,6 +350,7 @@ public JsonFactory(ObjectCodec oc) {
344350 _objectCodec = oc ;
345351 _quoteChar = DEFAULT_QUOTE_CHAR ;
346352 _streamReadConstraints = StreamReadConstraints .defaults ();
353+ _generatorDecorators = null ;
347354 }
348355
349356 /**
@@ -364,6 +371,7 @@ protected JsonFactory(JsonFactory src, ObjectCodec codec)
364371 _generatorFeatures = src ._generatorFeatures ;
365372 _inputDecorator = src ._inputDecorator ;
366373 _outputDecorator = src ._outputDecorator ;
374+ _generatorDecorators = _copy (src ._generatorDecorators );
367375 _streamReadConstraints = src ._streamReadConstraints == null ?
368376 StreamReadConstraints .defaults () : src ._streamReadConstraints ;
369377
@@ -390,6 +398,7 @@ public JsonFactory(JsonFactoryBuilder b) {
390398 _generatorFeatures = b ._streamWriteFeatures ;
391399 _inputDecorator = b ._inputDecorator ;
392400 _outputDecorator = b ._outputDecorator ;
401+ _generatorDecorators = _copy (b ._generatorDecorators );
393402 _streamReadConstraints = b ._streamReadConstraints == null ?
394403 StreamReadConstraints .defaults () : b ._streamReadConstraints ;
395404
@@ -398,7 +407,6 @@ public JsonFactory(JsonFactoryBuilder b) {
398407 _rootValueSeparator = b ._rootValueSeparator ;
399408 _maximumNonEscapedChar = b ._maximumNonEscapedChar ;
400409 _quoteChar = b ._quoteChar ;
401- _generatorDecorators = new ArrayList <>(b ._generatorDecorators );
402410 }
403411
404412 /**
@@ -417,6 +425,7 @@ protected JsonFactory(TSFBuilder<?,?> b, boolean bogus) {
417425 _generatorFeatures = b ._streamWriteFeatures ;
418426 _inputDecorator = b ._inputDecorator ;
419427 _outputDecorator = b ._outputDecorator ;
428+ _generatorDecorators = _copy (b ._generatorDecorators );
420429 _streamReadConstraints = b ._streamReadConstraints == null ?
421430 StreamReadConstraints .defaults () : b ._streamReadConstraints ;
422431
@@ -487,6 +496,14 @@ protected void _checkInvalidCopy(Class<?> exp)
487496 }
488497 }
489498
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+
490507 /*
491508 /**********************************************************
492509 /* Serializable overrides
@@ -1901,13 +1918,6 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
19011918 return _decorate (gen );
19021919 }
19031920
1904- private JsonGenerator _decorate (JsonGenerator result ) {
1905- for (JsonGeneratorDecorator decorator : _generatorDecorators ) {
1906- result = decorator .decorate (this , result );
1907- }
1908- return result ;
1909- }
1910-
19111921 /**
19121922 * Overridable factory method that actually instantiates generator for
19131923 * given {@link OutputStream} and context object, using UTF-8 encoding.
@@ -2008,6 +2018,25 @@ protected final Writer _decorate(Writer out, IOContext ctxt) throws IOException
20082018 return out ;
20092019 }
20102020
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+
20112040 /*
20122041 /**********************************************************
20132042 /* Internal factory methods, other
0 commit comments