diff --git a/CHANGES.md b/CHANGES.md index 82ee442eca..8f526dd8ab 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -21,6 +21,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default `gson` version to latest `2.13.1` -> `2.13.2`. ([#2615](https://github.com/diffplug/spotless/pull/2615)) * **BREAKING** Bump default `ktfmt` version to latest `0.53` -> `0.58` ([#2613](https://github.com/diffplug/spotless/pull/2613)) * use `TrailingCommaManagementStrategy` enum instead of `manageTrailingCommas` boolean configuration option +### Fixed +* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103)) ## [3.3.1] - 2025-07-21 ### Fixed diff --git a/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java b/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java index 71215eb4a3..0cd8d85849 100644 --- a/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java +++ b/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2023 DiffPlug + * Copyright 2021-2025 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,7 +23,6 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.util.DefaultIndenter; import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; -import com.fasterxml.jackson.core.util.Separators; import com.diffplug.spotless.FormatterFunc; import com.diffplug.spotless.json.JacksonJsonConfig; @@ -88,23 +87,23 @@ protected static class SpotlessJsonPrettyPrinter extends DefaultPrettyPrinter { public SpotlessJsonPrettyPrinter(boolean spaceBeforeSeparator) { this.spaceBeforeSeparator = spaceBeforeSeparator; + + if (_objectFieldValueSeparatorWithSpaces == null || _objectFieldValueSeparatorWithSpaces.isEmpty()) { + return; + } + + // Keep the behavior consistent even if Jackson changes default behavior + boolean startsWithSpace = Character.isWhitespace(_objectFieldValueSeparatorWithSpaces.charAt(0)); + if (spaceBeforeSeparator && !startsWithSpace) { + _objectFieldValueSeparatorWithSpaces = String.format(" %s", _objectFieldValueSeparatorWithSpaces); + } else if (!spaceBeforeSeparator && startsWithSpace) { + _objectFieldValueSeparatorWithSpaces = _objectFieldValueSeparatorWithSpaces.substring(1); + } } @Override public DefaultPrettyPrinter createInstance() { return new SpotlessJsonPrettyPrinter(spaceBeforeSeparator); } - - @Override - public DefaultPrettyPrinter withSeparators(Separators separators) { - this._separators = separators; - if (spaceBeforeSeparator) { - // This is Jackson default behavior - this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " "; - } else { - this._objectFieldValueSeparatorWithSpaces = separators.getObjectFieldValueSeparator() + " "; - } - return this; - } } } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 4b2b79f3fa..00bf9b6704 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -17,6 +17,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ### Fixed * Respect system gitconfig when performing git operations ([#2404](https://github.com/diffplug/spotless/issues/2404)) +* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103)) * `spotlessInstallGitPrePushHook` is now compatible with configuration cache. ([#2570](https://github.com/diffplug/spotless/pull/2570)) ## [7.2.1] - 2025-07-21 diff --git a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java index df0e5e6061..9d3c111b59 100644 --- a/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java +++ b/plugin-gradle/src/main/java/com/diffplug/gradle/spotless/JsonExtension.java @@ -168,6 +168,11 @@ public JacksonJsonGradleConfig jsonFeature(String feature, boolean toggle) { return this; } + public JacksonJsonGradleConfig setSpaceBeforeSeparator(boolean value) { + jacksonConfig.setSpaceBeforeSeparator(value); + return this; + } + @Override public JacksonJsonGradleConfig self() { return this; diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index bc7a38764d..641cdec013 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -14,6 +14,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( * Bump default `gson` version to latest `2.13.1` -> `2.13.2`. ([#2615](https://github.com/diffplug/spotless/pull/2615)) * **BREAKING** Bump default `ktfmt` version to latest `0.53` -> `0.58` ([#2613](https://github.com/diffplug/spotless/pull/2613)) * use `TrailingCommaManagementStrategy` enum instead of `manageTrailingCommas` boolean configuration option +### Fixed +* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103)) ## [2.46.1] - 2025-07-21 ### Fixed diff --git a/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json b/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json index 25ce5dd094..55713c0145 100644 --- a/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json +++ b/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json @@ -1,19 +1,19 @@ { - "A" : 1, - "X" : 2, - "_arraysNotSorted" : [ 3, 2, 1 ], - "_objectsInArraysAreSorted" : [ { - "a" : 1, - "b" : 2 + "A": 1, + "X": 2, + "_arraysNotSorted": [ 3, 2, 1 ], + "_objectsInArraysAreSorted": [ { + "a": 1, + "b": 2 } ], - "a" : 3, - "c" : 4, - "x" : 5, - "z" : { - "A" : 1, - "X" : 2, - "a" : 3, - "c" : 4, - "x" : 5 + "a": 3, + "c": 4, + "x": 5, + "z": { + "A": 1, + "X": 2, + "a": 3, + "c": 4, + "x": 5 } } \ No newline at end of file