Skip to content

Commit 9a1f849

Browse files
authored
Fix spaceBeforeSeparator in Jackson Gradle plugin (#2103)
2 parents 6991270 + e039fce commit 9a1f849

File tree

6 files changed

+38
-29
lines changed

6 files changed

+38
-29
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
2121
* Bump default `gson` version to latest `2.13.1` -> `2.13.2`. ([#2615](https://github.com/diffplug/spotless/pull/2615))
2222
* **BREAKING** Bump default `ktfmt` version to latest `0.53` -> `0.58` ([#2613](https://github.com/diffplug/spotless/pull/2613))
2323
* use `TrailingCommaManagementStrategy` enum instead of `manageTrailingCommas` boolean configuration option
24+
### Fixed
25+
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
2426

2527
## [3.3.1] - 2025-07-21
2628
### Fixed

lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021-2023 DiffPlug
2+
* Copyright 2021-2025 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,6 @@
2323
import com.fasterxml.jackson.core.JsonGenerator;
2424
import com.fasterxml.jackson.core.util.DefaultIndenter;
2525
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
26-
import com.fasterxml.jackson.core.util.Separators;
2726

2827
import com.diffplug.spotless.FormatterFunc;
2928
import com.diffplug.spotless.json.JacksonJsonConfig;
@@ -88,23 +87,23 @@ protected static class SpotlessJsonPrettyPrinter extends DefaultPrettyPrinter {
8887

8988
public SpotlessJsonPrettyPrinter(boolean spaceBeforeSeparator) {
9089
this.spaceBeforeSeparator = spaceBeforeSeparator;
90+
91+
if (_objectFieldValueSeparatorWithSpaces == null || _objectFieldValueSeparatorWithSpaces.isEmpty()) {
92+
return;
93+
}
94+
95+
// Keep the behavior consistent even if Jackson changes default behavior
96+
boolean startsWithSpace = Character.isWhitespace(_objectFieldValueSeparatorWithSpaces.charAt(0));
97+
if (spaceBeforeSeparator && !startsWithSpace) {
98+
_objectFieldValueSeparatorWithSpaces = String.format(" %s", _objectFieldValueSeparatorWithSpaces);
99+
} else if (!spaceBeforeSeparator && startsWithSpace) {
100+
_objectFieldValueSeparatorWithSpaces = _objectFieldValueSeparatorWithSpaces.substring(1);
101+
}
91102
}
92103

93104
@Override
94105
public DefaultPrettyPrinter createInstance() {
95106
return new SpotlessJsonPrettyPrinter(spaceBeforeSeparator);
96107
}
97-
98-
@Override
99-
public DefaultPrettyPrinter withSeparators(Separators separators) {
100-
this._separators = separators;
101-
if (spaceBeforeSeparator) {
102-
// This is Jackson default behavior
103-
this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
104-
} else {
105-
this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " ";
106-
}
107-
return this;
108-
}
109108
}
110109
}

plugin-gradle/CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1717

1818
### Fixed
1919
* Respect system gitconfig when performing git operations ([#2404](https://github.com/diffplug/spotless/issues/2404))
20+
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
2021
* `spotlessInstallGitPrePushHook` is now compatible with configuration cache. ([#2570](https://github.com/diffplug/spotless/pull/2570))
2122

2223
## [7.2.1] - 2025-07-21

plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,11 @@ public JacksonJsonGradleConfig jsonFeature(String feature, boolean toggle) {
168168
return this;
169169
}
170170

171+
public JacksonJsonGradleConfig setSpaceBeforeSeparator(boolean value) {
172+
jacksonConfig.setSpaceBeforeSeparator(value);
173+
return this;
174+
}
175+
171176
@Override
172177
public JacksonJsonGradleConfig self() {
173178
return this;

plugin-maven/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1414
* Bump default `gson` version to latest `2.13.1` -> `2.13.2`. ([#2615](https://github.com/diffplug/spotless/pull/2615))
1515
* **BREAKING** Bump default `ktfmt` version to latest `0.53` -> `0.58` ([#2613](https://github.com/diffplug/spotless/pull/2613))
1616
* use `TrailingCommaManagementStrategy` enum instead of `manageTrailingCommas` boolean configuration option
17+
### Fixed
18+
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
1719

1820
## [2.46.1] - 2025-07-21
1921
### Fixed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"A" : 1,
3-
"X" : 2,
4-
"_arraysNotSorted" : [ 3, 2, 1 ],
5-
"_objectsInArraysAreSorted" : [ {
6-
"a" : 1,
7-
"b" : 2
2+
"A": 1,
3+
"X": 2,
4+
"_arraysNotSorted": [ 3, 2, 1 ],
5+
"_objectsInArraysAreSorted": [ {
6+
"a": 1,
7+
"b": 2
88
} ],
9-
"a" : 3,
10-
"c" : 4,
11-
"x" : 5,
12-
"z" : {
13-
"A" : 1,
14-
"X" : 2,
15-
"a" : 3,
16-
"c" : 4,
17-
"x" : 5
9+
"a": 3,
10+
"c": 4,
11+
"x": 5,
12+
"z": {
13+
"A": 1,
14+
"X": 2,
15+
"a": 3,
16+
"c": 4,
17+
"x": 5
1818
}
1919
}

0 commit comments

Comments
 (0)