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

support configuring jackson JsonParser. Feature #2272

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.Separators;
Expand Down Expand Up @@ -64,7 +65,12 @@ protected JsonFactory makeJsonFactory() {

jsonFactory.configure(feature, toggle);
});
jacksonConfig.getJsonParserFeatureToToggle().forEach((rawFeature, toggle) -> {
// https://stackoverflow.com/questions/3735927/java-instantiating-an-enum-using-reflection
JsonParser.Feature feature = JsonParser.Feature.valueOf(rawFeature);

jsonFactory.configure(feature, toggle);
});
return jsonFactory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class JacksonJsonConfig extends JacksonConfig {

protected Map<String, Boolean> jsonFeatureToToggle = new LinkedHashMap<>();
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved

protected Map<String, Boolean> jsonParserFeatureToToggle = new LinkedHashMap<>();

// https://github.com/revelc/formatter-maven-plugin/pull/280
// By default, Jackson adds a ' ' before separator, which is not standard with most IDE/JSON libraries
protected boolean spaceBeforeSeparator = false;
Expand All @@ -35,6 +37,10 @@ public Map<String, Boolean> getJsonFeatureToToggle() {
return Collections.unmodifiableMap(jsonFeatureToToggle);
}

public Map<String, Boolean> getJsonParserFeatureToToggle() {
return Collections.unmodifiableMap(jsonParserFeatureToToggle);
}

/**
* Refers to com.fasterxml.jackson.core.JsonGenerator.Feature
*/
Expand All @@ -49,6 +55,13 @@ public void appendJsonFeatureToToggle(Map<String, Boolean> features) {
this.jsonFeatureToToggle.putAll(features);
}

/**
* Refers to com.fasterxml.jackson.core.JsonParser.Feature
*/
public void appendJsonParserFeatureToToggle(Map<String, Boolean> features) {
this.jsonParserFeatureToToggle.putAll(features);
}

public boolean isSpaceBeforeSeparator() {
return spaceBeforeSeparator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ public JacksonJsonGradleConfig jsonFeature(String feature, boolean toggle) {
return this;
}

/**
* Refers to com.fasterxml.jackson.core.JsonParser.Feature
*/
public JacksonJsonGradleConfig jsonParserFeature(String feature, boolean toggle) {
this.jacksonConfig.appendJsonParserFeatureToToggle(Collections.singletonMap(feature, toggle));
formatExtension.replaceStep(createStep());
return this;
}

@Override
public JacksonJsonGradleConfig self() {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ public class JacksonJson implements FormatterStepFactory {
@Parameter
private Map<String, Boolean> jsonFeatures = Collections.emptyMap();
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved

@Parameter
private Map<String, Boolean> jsonParserFeatures = Collections.emptyMap();

@Override
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
JacksonJsonConfig jacksonConfig = new JacksonJsonConfig();

jacksonConfig.appendFeatureToToggle(features);
jacksonConfig.appendJsonFeatureToToggle(jsonFeatures);
jacksonConfig.appendJsonParserFeatureToToggle(jsonParserFeatures);
jacksonConfig.setSpaceBeforeSeparator(spaceBeforeSeparator);

return JacksonJsonStep
Expand Down
Loading