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

Fix spaceBeforeSeparator in Jackson Gradle plugin #2103

Open
wants to merge 8 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `cdt` version to latest `11.3` -> `11.6`. ([#2179](https://github.com/diffplug/spotless/pull/2179))
* Bump default `gson` version to latest `2.10.1` -> `2.11.0`. ([#2128](https://github.com/diffplug/spotless/pull/2128))
### Fixed
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
### Added
* Add option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 DiffPlug
* Copyright 2021-2024 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 @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
1 change: 1 addition & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `cdt` version to latest `11.3` -> `11.6`. ([#2179](https://github.com/diffplug/spotless/pull/2179))
* Bump default `gson` version to latest `2.10.1` -> `2.11.0`. ([#2128](https://github.com/diffplug/spotless/pull/2128))
### Fixed
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
### Added
* Add option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,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;
Expand Down
1 change: 1 addition & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
* Bump default `cdt` version to latest `11.3` -> `11.6`. ([#2179](https://github.com/diffplug/spotless/pull/2179))
* Bump default `gson` version to latest `2.10.1` -> `2.11.0`. ([#2128](https://github.com/diffplug/spotless/pull/2128))
### Fixed
* Fix `spaceBeforeSeparator` in Jackson formatter. ([#2103](https://github.com/diffplug/spotless/pull/2103))
* Fix compatibility issue introduced by `ktfmt` `0.51`. ([#2172](https://github.com/diffplug/spotless/issues/2172))
### Added
* Add option `manageTrailingCommas` to `ktfmt`. ([#2177](https://github.com/diffplug/spotless/pull/2177))
Expand Down
Loading