From 5e728be5dbad1a66f04b19f62b4483c186bb727a Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Tue, 3 Jan 2023 22:33:12 +0400 Subject: [PATCH 1/6] Initial iteration --- plugin-maven/README.md | 50 ++++++++++++++++++- .../diffplug/spotless/maven/json/Gson.java | 45 +++++++++++++++++ .../diffplug/spotless/maven/json/Json.java | 47 +++++++++++++++++ .../diffplug/spotless/maven/json/Simple.java | 35 +++++++++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java create mode 100644 plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.java diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 80be3dbc37..8be4149705 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` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/6.12.1/com/diffplug/gradle/spotless/JsonExtension.html), [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..9091157637 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Gson.java @@ -0,0 +1,45 @@ +/* + * Copyright 2021 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..4cd4e88a7d --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java @@ -0,0 +1,47 @@ +/* + * Copyright 2021 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..5926d22282 --- /dev/null +++ b/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Simple.java @@ -0,0 +1,35 @@ +/* + * Copyright 2021 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()); + } +} From a7f969c8a950b33fd609b93a77b9032fb191cf30 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Tue, 3 Jan 2023 22:34:34 +0400 Subject: [PATCH 2/6] Fix copyright --- .../src/main/java/com/diffplug/spotless/maven/json/Gson.java | 2 +- .../src/main/java/com/diffplug/spotless/maven/json/Json.java | 2 +- .../src/main/java/com/diffplug/spotless/maven/json/Simple.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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 index 9091157637..7962ecb1f7 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * 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. 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 index 4cd4e88a7d..27f9a0b82a 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * 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. 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 index 5926d22282..f1cc114da2 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2021 DiffPlug + * 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. From fc938dfdd8d800ea37ac79d456e66f6a51250189 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 4 Jan 2023 11:27:01 +0400 Subject: [PATCH 3/6] Add changes note, Fix style --- plugin-maven/CHANGES.md | 1 + plugin-maven/README.md | 12 +++--- .../diffplug/spotless/maven/json/Json.java | 2 +- .../spotless/maven/json/JsonTest.java | 40 +++++++++++++++++++ 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 plugin-maven/src/test/java/com/diffplug/spotless/maven/json/JsonTest.java 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 8be4149705..70f3dfe1dc 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -724,8 +724,8 @@ Uses a JSON pretty-printer that optionally allows configuring the number of spac ```xml - 4 - + 4 + ``` ### Gson @@ -734,10 +734,10 @@ Uses Google Gson to also allow sorting by keys besides custom indentation - usef ```xml - 4 - false - false - 2.8.1 + 4 + false + false + 2.8.1 ``` 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 index 27f9a0b82a..c326525d0c 100644 --- 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 @@ -25,7 +25,7 @@ */ public class Json extends FormatterFactory { public static final int DEFAULT_INDENTATION = 4; - + @Override public Set defaultIncludes() { return Collections.emptySet(); 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..b7e666190a --- /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 { + writePomWithPomSteps(""); + + setFile("json_test.json").toResource("json/json_dirty.json"); + mavenRunner().withArguments("spotless:apply").runNoError().error(); + assertFile("json_test.json").sameAsResource("json/json_clean_default.json"); + } + + @Test + public void testFormatJson_WithGson_defaultConfig() throws Exception { + writePomWithPomSteps(""); + + setFile("json_test.json").toResource("json/json_dirty.json"); + mavenRunner().withArguments("spotless:apply").runNoError().error(); + assertFile("json_test.json").sameAsResource("json/json_clean_default.json"); + } +} From f01b03891d980e06f215ae2706c39a5f2d20753e Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 4 Jan 2023 18:24:17 +0400 Subject: [PATCH 4/6] Fix tests --- .../spotless/maven/MavenIntegrationHarness.java | 4 ++++ .../com/diffplug/spotless/maven/json/JsonTest.java | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) 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..1976d577bb 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 @@ -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 index b7e666190a..fe6560cbec 100644 --- 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 @@ -22,19 +22,19 @@ public class JsonTest extends MavenIntegrationHarness { @Test public void testFormatJson_WithSimple_defaultConfig() throws Exception { - writePomWithPomSteps(""); + writePomWithJsonSteps(""); - setFile("json_test.json").toResource("json/json_dirty.json"); + setFile("json_test.json").toResource("json/sortByKeysBefore.json"); mavenRunner().withArguments("spotless:apply").runNoError().error(); - assertFile("json_test.json").sameAsResource("json/json_clean_default.json"); + assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json"); } @Test public void testFormatJson_WithGson_defaultConfig() throws Exception { - writePomWithPomSteps(""); + writePomWithJsonSteps(""); - setFile("json_test.json").toResource("json/json_dirty.json"); + setFile("json_test.json").toResource("json/sortByKeysBefore.json"); mavenRunner().withArguments("spotless:apply").runNoError().error(); - assertFile("json_test.json").sameAsResource("json/json_clean_default.json"); + assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json"); } } From 1d2290d61e019edc01f86c2ee838a00107935ac4 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 4 Jan 2023 18:29:05 +0400 Subject: [PATCH 5/6] Fix README --- plugin-maven/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-maven/README.md b/plugin-maven/README.md index 70f3dfe1dc..dcc18a5dd6 100644 --- a/plugin-maven/README.md +++ b/plugin-maven/README.md @@ -703,7 +703,7 @@ For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#n ## JSON -- `com.diffplug.spotless.maven.json.Json` [javadoc](https://javadoc.io/doc/com.diffplug.spotless/spotless-plugin-gradle/6.12.1/com/diffplug/gradle/spotless/JsonExtension.html), [code](https://github.com/diffplug/spotless/blob/main/plugin-maven/src/main/java/com/diffplug/spotless/maven/json/Json.java) +- `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 From a4899012b2cc08c176264567e829979de1d33095 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Wed, 4 Jan 2023 18:47:39 +0400 Subject: [PATCH 6/6] Fix style --- .../com/diffplug/spotless/maven/MavenIntegrationHarness.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1976d577bb..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.