Skip to content

Commit edc944e

Browse files
authored
Support to register decorators for JsonGenerator in the builder and factory. (#1051)
1 parent 09e4f6a commit edc944e

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

src/main/java/com/fasterxml/jackson/core/JsonFactory.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.io.*;
88
import java.lang.ref.SoftReference;
99
import java.net.URL;
10+
import java.util.ArrayList;
11+
import java.util.List;
1012
import java.util.Objects;
1113

1214
import com.fasterxml.jackson.core.format.InputAccessor;
@@ -317,6 +319,9 @@ public static int collectDefaults() {
317319
*/
318320
protected final char _quoteChar;
319321

322+
/** @since 2.16 */
323+
protected List<JsonGeneratorDecorator> _generatorDecorators = new ArrayList<>();
324+
320325
/*
321326
/**********************************************************
322327
/* Construction
@@ -393,6 +398,7 @@ public JsonFactory(JsonFactoryBuilder b) {
393398
_rootValueSeparator = b._rootValueSeparator;
394399
_maximumNonEscapedChar = b._maximumNonEscapedChar;
395400
_quoteChar = b._quoteChar;
401+
_generatorDecorators = new ArrayList<>(b._generatorDecorators);
396402
}
397403

398404
/**
@@ -1892,7 +1898,14 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
18921898
if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
18931899
gen.setRootValueSeparator(rootSep);
18941900
}
1895-
return gen;
1901+
return _decorate(gen);
1902+
}
1903+
1904+
private JsonGenerator _decorate(JsonGenerator result) {
1905+
for(JsonGeneratorDecorator decorator : _generatorDecorators) {
1906+
result = decorator.decorate(this, result);
1907+
}
1908+
return result;
18961909
}
18971910

18981911
/**
@@ -1925,7 +1938,7 @@ protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) t
19251938
if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
19261939
gen.setRootValueSeparator(rootSep);
19271940
}
1928-
return gen;
1941+
return _decorate(gen);
19291942
}
19301943

19311944
protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.fasterxml.jackson.core;
2+
3+
public interface JsonGeneratorDecorator {
4+
/**
5+
* Allow to decorate {@link JsonGenerator} instances returned by {@link JsonFactory}.
6+
*
7+
* @since 2.16
8+
* @param factory The factory which was used to build the original generator
9+
* @param generator The generator to decorate. This might already be a decorated instance, not the original.
10+
* @return decorated generator
11+
*/
12+
JsonGenerator decorate(JsonFactory factory, JsonGenerator generator);
13+
}

src/main/java/com/fasterxml/jackson/core/TSFBuilder.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.fasterxml.jackson.core;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
import com.fasterxml.jackson.core.io.InputDecorator;
47
import com.fasterxml.jackson.core.io.OutputDecorator;
58
import com.fasterxml.jackson.core.json.JsonReadFeature;
@@ -84,6 +87,9 @@ public abstract class TSFBuilder<F extends JsonFactory,
8487
*/
8588
protected StreamReadConstraints _streamReadConstraints;
8689

90+
/** @since 2.16 */
91+
protected List<JsonGeneratorDecorator> _generatorDecorators = new ArrayList<>();
92+
8793
/*
8894
/**********************************************************************
8995
/* Construction
@@ -284,6 +290,11 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {
284290

285291
// // // Other methods
286292

293+
public B decorateWith(JsonGeneratorDecorator decorator) {
294+
_generatorDecorators.add(decorator);
295+
return _this();
296+
}
297+
287298
/**
288299
* Method for constructing actual {@link TokenStreamFactory} instance, given
289300
* configuration.

0 commit comments

Comments
 (0)