Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JsonGeneratorDecorator to allow decorating JsonGenerators #1051

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/main/java/com/fasterxml/jackson/core/JsonFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.*;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

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

/** @since 2.16 */
protected List<JsonGeneratorDecorator> _generatorDecorators = new ArrayList<>();

/*
/**********************************************************
/* Construction
Expand Down Expand Up @@ -393,6 +398,7 @@ public JsonFactory(JsonFactoryBuilder b) {
_rootValueSeparator = b._rootValueSeparator;
_maximumNonEscapedChar = b._maximumNonEscapedChar;
_quoteChar = b._quoteChar;
_generatorDecorators = new ArrayList<>(b._generatorDecorators);
}

/**
Expand Down Expand Up @@ -1892,7 +1898,14 @@ protected JsonGenerator _createGenerator(Writer out, IOContext ctxt) throws IOEx
if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
gen.setRootValueSeparator(rootSep);
}
return gen;
return _decorate(gen);
}

private JsonGenerator _decorate(JsonGenerator result) {
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
for(JsonGeneratorDecorator decorator : _generatorDecorators) {
result = decorator.decorate(this, result);
}
return result;
}

/**
Expand Down Expand Up @@ -1925,7 +1938,7 @@ protected JsonGenerator _createUTF8Generator(OutputStream out, IOContext ctxt) t
if (rootSep != DEFAULT_ROOT_VALUE_SEPARATOR) {
gen.setRootValueSeparator(rootSep);
}
return gen;
return _decorate(gen);
}

protected Writer _createWriter(OutputStream out, JsonEncoding enc, IOContext ctxt) throws IOException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.fasterxml.jackson.core;

public interface JsonGeneratorDecorator {
/**
* Allow to decorate {@link JsonGenerator} instances returned by {@link JsonFactory}.
*
* @since 2.16
* @param factory The factory which was used to build the original generator
* @param generator The generator to decorate. This might already be a decorated instance, not the original.
* @return decorated generator
*/
JsonGenerator decorate(JsonFactory factory, JsonGenerator generator);
}
11 changes: 11 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/TSFBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.fasterxml.jackson.core;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.io.InputDecorator;
import com.fasterxml.jackson.core.io.OutputDecorator;
import com.fasterxml.jackson.core.json.JsonReadFeature;
Expand Down Expand Up @@ -84,6 +87,9 @@ public abstract class TSFBuilder<F extends JsonFactory,
*/
protected StreamReadConstraints _streamReadConstraints;

/** @since 2.16 */
protected List<JsonGeneratorDecorator> _generatorDecorators = new ArrayList<>();

/*
/**********************************************************************
/* Construction
Expand Down Expand Up @@ -284,6 +290,11 @@ public B streamReadConstraints(StreamReadConstraints streamReadConstraints) {

// // // Other methods

public B decorateWith(JsonGeneratorDecorator decorator) {
_generatorDecorators.add(decorator);
return _this();
}

/**
* Method for constructing actual {@link TokenStreamFactory} instance, given
* configuration.
Expand Down