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 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
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
22 changes: 22 additions & 0 deletions testlib/src/main/resources/json/objectContainingComments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2023"],
"module": "ESNext",
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["vite.config.ts"]
}
18 changes: 18 additions & 0 deletions testlib/src/main/resources/json/objectContainingCommentsAfter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"compilerOptions" : {
"target" : "ES2022",
"lib" : [ "ES2023" ],
"module" : "ESNext",
"skipLibCheck" : true,
"moduleResolution" : "bundler",
"allowImportingTsExtensions" : true,
"isolatedModules" : true,
"moduleDetection" : "force",
"noEmit" : true,
"strict" : true,
"noUnusedLocals" : true,
"noUnusedParameters" : true,
"noFallthroughCasesInSwitch" : true
},
"include" : [ "vite.config.ts" ]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.diffplug.spotless.StepHarness;
import com.diffplug.spotless.TestProvisioner;

import java.util.Map;

import static com.diffplug.spotless.json.JacksonJsonStep.defaultVersion;

class JacksonJsonStepTest {
@Test
void canSetCustomIndentationLevel() {
Expand All @@ -31,4 +35,16 @@ void canSetCustomIndentationLevel() {
String after = "json/singletonArrayAfter_Jackson.json";
stepHarness.testResource(before, after);
}

@Test
void canSetJsonParserFeature() {
JacksonJsonConfig jacksonJsonConfig = new JacksonJsonConfig();
jacksonJsonConfig.appendJsonParserFeatureToToggle(Map.of("ALLOW_COMMENTS", true));
FormatterStep step = JacksonJsonStep.create(jacksonJsonConfig, defaultVersion(), TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStepNoRoundtrip(step);

String before = "json/objectContainingComments.json";
String after = "json/objectContainingCommentsAfter.json";
stepHarness.testResource(before, after);
}
}
Loading