Skip to content
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
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2025 DiffPlug
* Copyright 2021-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -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;

Expand Down Expand Up @@ -65,6 +66,13 @@ 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
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2025 DiffPlug
* Copyright 2023-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,6 +29,8 @@ public class JacksonJsonConfig extends JacksonConfig {

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

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;
Expand All @@ -37,6 +39,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 @@ -51,6 +57,20 @@ public void appendJsonFeatureToToggle(Map<String, Boolean> features) {
this.jsonFeatureToToggle.putAll(features);
}

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

/**
* 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
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
- Fix `Cannot set the value of task ... property 'taskService'` when Spotless is applied to sibling projects but not to their parent, a regression since `8.3.0`. Applying it to the root project with `apply false` is no longer necessary. ([#2877](https://github.com/diffplug/spotless/issues/2877))
### Changes
- README: example for formatting Dockerfiles with prettier and `prettier-plugin-sh`. ([#3158](https://github.com/diffplug/spotless/issues/3158))
### Added
- `jacksonJson`: new `jsonParserFeature(feature, toggle)` method to toggle any `com.fasterxml.jackson.core.JsonParser.Feature`, such as `ALLOW_COMMENTS` for JSON files that contain comments. ([#2271](https://github.com/diffplug/spotless/issues/2271))

## [8.12.0] - 2026-11-21
### Added
Expand Down
1 change: 1 addition & 0 deletions plugin-gradle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ spotless {
.feature('ORDER_MAP_ENTRIES_BY_KEYS', true) // optional: false by default
.feature('ANY_OTHER_FEATURE', true|false) // optional: any SerializationFeature can be toggled on or off
.jsonFeature('ANY_OTHER_FEATURE', true|false) // any JsonGenerator.Feature can be toggled on or off
.jsonParserFeature('ALLOW_COMMENTS', true) // optional: any JsonParser.Feature can be toggled on or off
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,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(Map.of(feature, toggle));
formatExtension.replaceStep(createStep());
return this;
}

public JacksonJsonGradleConfig setSpaceBeforeSeparator(boolean value) {
jacksonConfig.setSpaceBeforeSeparator(value);
return this;
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
- Fix npm/node executable discovery treating a directory (e.g. an `npm` folder inside `NODE_PATH`) as an executable. ([#3190](https://github.com/diffplug/spotless/issues/3190))
### Changes
- README: example for formatting Dockerfiles with prettier and `prettier-plugin-sh`. ([#3158](https://github.com/diffplug/spotless/issues/3158))
### Added
- `jacksonJson`: new `<jsonParserFeatures>` option to toggle any `com.fasterxml.jackson.core.JsonParser.Feature`, such as `ALLOW_COMMENTS` for JSON files that contain comments. ([#2271](https://github.com/diffplug/spotless/issues/2271))

## [3.12.0] - 2026-11-21
### Changes
Expand Down
3 changes: 3 additions & 0 deletions plugin-maven/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,9 @@ Uses Jackson for formatting.
<QUOTE_FIELD_NAMES>false</QUOTE_FIELD_NAMES> <!-- false by default -->
<ANY_OTHER_FEATURE>true|false</ANY_OTHER_FEATURE> <!-- any JsonGenerator.Feature can be toggled on or off -->
</jsonFeatures>
<jsonParserFeatures> <!-- optional: any JsonParser.Feature can be toggled on or off -->
<ALLOW_COMMENTS>true</ALLOW_COMMENTS>
</jsonParserFeatures>
<spaceBeforeSeparator>false</spaceBeforeSeparator> <!-- optional: false by default -->
</jackson>
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 DiffPlug
* Copyright 2023-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,12 +44,16 @@ public class JacksonJson implements FormatterStepFactory {
@Parameter
private Map<String, Boolean> jsonFeatures = Collections.emptyMap();

@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
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2024 DiffPlug
* Copyright 2021-2026 DiffPlug
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@
*/
package com.diffplug.spotless.json;

import java.util.Map;

import org.junit.jupiter.api.Test;

import com.diffplug.spotless.FormatterStep;
Expand All @@ -31,4 +33,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, JacksonJsonStep.defaultVersion(), TestProvisioner.mavenCentral());
StepHarness stepHarness = StepHarness.forStepNoRoundtrip(step);

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