diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 84e06aa847..b15e005a9b 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [2.29.0] - 2023-01-02 ### Added * Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413)) +* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446)) ### Fixed * Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426)) * Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430)) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 80be3dbc37..dcc18a5dd6 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check - [Maven Pom](#maven-pom) ([sortPom](#sortpom)) - [Markdown](#markdown) ([flexmark](#flexmark)) - [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier)) + - [JSON](#json) - Multiple languages - [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection)) - [eclipse web tools platform](#eclipse-web-tools-platform) @@ -431,7 +432,7 @@ Groovy-Eclipse formatting errors/warnings lead per default to a build failure. T - src/native/** + src/native/** @@ -700,6 +701,53 @@ The auto-discovery of config files (up the file tree) will not work when using t For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt. +## JSON + +- `com.diffplug.spotless.maven.json.Json` [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/json.java) + +```xml + + + + src/**/*.json + + + + + + +``` + +### simple + +Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects: + +```xml + + 4 + +``` + +### Gson + +Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files. + +```xml + + 4 + false + false + 2.8.1 + +``` + +Notes: +* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either +all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former. +* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the +[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String)) +for details. + ## Prettier diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java new file mode 100644 index 0000000000..7962ecb1f7 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java @@ -0,0 +1,45 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.json; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.json.gson.GsonStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class Gson implements FormatterStepFactory { + private static final String DEFAULT_GSON_VERSION = "2.8.9"; + + @Parameter + int indentSpaces = Json.DEFAULT_INDENTATION; + + @Parameter + boolean sortByKeys = false; + + @Parameter + boolean escapeHtml = false; + + @Parameter + String version = DEFAULT_GSON_VERSION; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + int indentSpaces = this.indentSpaces; + return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner()); + } +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java new file mode 100644 index 0000000000..c326525d0c --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java @@ -0,0 +1,47 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.json; + +import java.util.Collections; +import java.util.Set; + +import com.diffplug.spotless.maven.FormatterFactory; + +/** + * A {@link FormatterFactory} implementation that corresponds to {@code ...} configuration element. + */ +public class Json extends FormatterFactory { + public static final int DEFAULT_INDENTATION = 4; + + @Override + public Set defaultIncludes() { + return Collections.emptySet(); + } + + @Override + public String licenseHeaderDelimiter() { + return null; + } + + public void addSimple(Simple simple) { + addStepFactory(simple); + } + + public void addGson(Gson gson) { + addStepFactory(gson); + } + +} diff --git a/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.java b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.java new file mode 100644 index 0000000000..f1cc114da2 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.java @@ -0,0 +1,35 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.json; + +import org.apache.maven.plugins.annotations.Parameter; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.json.JsonSimpleStep; +import com.diffplug.spotless.maven.FormatterStepConfig; +import com.diffplug.spotless.maven.FormatterStepFactory; + +public class Simple implements FormatterStepFactory { + + @Parameter + int indentSpaces = Json.DEFAULT_INDENTATION; + + @Override + public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) { + int indentSpaces = this.indentSpaces; + return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner()); + } +} diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java index 939940a315..9e9495da65 100644 --- a/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java @@ -1,5 +1,5 @@ /* - * Copyright 2016-2022 DiffPlug + * Copyright 2016-2023 DiffPlug * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -156,6 +156,10 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException { writePom(groupWithSteps("markdown", including("**/*.md"), steps)); } + protected void writePomWithJsonSteps(String... steps) throws IOException { + writePom(groupWithSteps("json", including("**/*.json"), steps)); + } + protected void writePom(String... configuration) throws IOException { writePom(null, configuration, null); } diff --git a/plugin-maven/src/test/java/com/diffplug/spotless/maven/json/JsonTest.java b/plugin-maven/src/test/java/com/diffplug/spotless/maven/json/JsonTest.java new file mode 100644 index 0000000000..fe6560cbec --- /dev/null +++ b/plugin-maven/src/test/java/com/diffplug/spotless/maven/json/JsonTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2023 DiffPlug + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.diffplug.spotless.maven.json; + +import org.junit.jupiter.api.Test; + +import com.diffplug.spotless.maven.MavenIntegrationHarness; + +public class JsonTest extends MavenIntegrationHarness { + @Test + public void testFormatJson_WithSimple_defaultConfig() throws Exception { + writePomWithJsonSteps(""); + + setFile("json_test.json").toResource("json/sortByKeysBefore.json"); + mavenRunner().withArguments("spotless:apply").runNoError().error(); + assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json"); + } + + @Test + public void testFormatJson_WithGson_defaultConfig() throws Exception { + writePomWithJsonSteps(""); + + setFile("json_test.json").toResource("json/sortByKeysBefore.json"); + mavenRunner().withArguments("spotless:apply").runNoError().error(); + assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json"); + } +}