Skip to content

Commit 2901edb

Browse files
authored
Enable json in maven plugin (#1446)
2 parents 0d7c864 + a489901 commit 2901edb

File tree

7 files changed

+222
-2
lines changed

7 files changed

+222
-2
lines changed

plugin-maven/CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
77
## [2.29.0] - 2023-01-02
88
### Added
99
* Added support for M2E's incremental compilation ([#1414](https://github.com/diffplug/spotless/pull/1414) fixes [#1413](https://github.com/diffplug/spotless/issues/1413))
10+
* Add JSON support ([#1446](https://github.com/diffplug/spotless/pull/1446))
1011
### Fixed
1112
* Improve memory usage when using git ratchet ([#1426](https://github.com/diffplug/spotless/pull/1426))
1213
* Support `ktlint` 0.48+ ([#1432](https://github.com/diffplug/spotless/pull/1432)) fixes ([#1430](https://github.com/diffplug/spotless/issues/1430))

plugin-maven/README.md

+49-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ user@machine repo % mvn spotless:check
5858
- [Maven Pom](#maven-pom) ([sortPom](#sortpom))
5959
- [Markdown](#markdown) ([flexmark](#flexmark))
6060
- [Typescript](#typescript) ([tsfmt](#tsfmt), [prettier](#prettier))
61+
- [JSON](#json)
6162
- Multiple languages
6263
- [Prettier](#prettier) ([plugins](#prettier-plugins), [npm detection](#npm-detection), [`.npmrc` detection](#npmrc-detection))
6364
- [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
431432
<configuration>
432433
<cpp>
433434
<includes> <!-- You have to set the target manually -->
434-
<include>src/native/**</inclue>
435+
<include>src/native/**</include>
435436
</includes>
436437

437438
<eclipseCdt /> <!-- has its own section below -->
@@ -700,6 +701,53 @@ The auto-discovery of config files (up the file tree) will not work when using t
700701

701702
For details, see the [npm detection](#npm-detection) and [`.npmrc` detection](#npmrc-detection) sections of prettier, which apply also to tsfmt.
702703

704+
## JSON
705+
706+
- `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)
707+
708+
```xml
709+
<configuration>
710+
<json>
711+
<includes> <!-- You have to set the target manually -->
712+
<include>src/**/*.json</include>
713+
</includes>
714+
715+
<simple /> <!-- has its own section below -->
716+
<gson /> <!-- has its own section below -->
717+
</json>
718+
</configuration>
719+
```
720+
721+
### simple
722+
723+
Uses a JSON pretty-printer that optionally allows configuring the number of spaces that are used to pretty print objects:
724+
725+
```xml
726+
<simple>
727+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
728+
</simple>
729+
```
730+
731+
### Gson
732+
733+
Uses Google Gson to also allow sorting by keys besides custom indentation - useful for i18n files.
734+
735+
```xml
736+
<gson>
737+
<indentSpaces>4</indentSpaces> <!-- optional: specify the number of spaces to use -->
738+
<sortByKeys>false</sortByKeys> <!-- optional: sort JSON by its keys -->
739+
<escapeHtml>false</indentSpaces> <!-- optional: escape HTML in values -->
740+
<version>2.8.1</version> <!-- optional: specify version -->
741+
</gson>
742+
```
743+
744+
Notes:
745+
* There's no option in Gson to leave HTML as-is (i.e. escaped HTML would remain escaped, raw would remain raw). Either
746+
all HTML characters are written escaped or none. Set `escapeHtml` if you prefer the former.
747+
* `sortByKeys` will apply lexicographic order on the keys of the input JSON. See the
748+
[javadoc of String](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#compareTo(java.lang.String))
749+
for details.
750+
703751
<a name="applying-prettier-to-javascript--flow--typescript--css--scss--less--jsx--graphql--yaml--etc"></a>
704752

705753
## Prettier
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.gson.GsonStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Gson implements FormatterStepFactory {
26+
private static final String DEFAULT_GSON_VERSION = "2.8.9";
27+
28+
@Parameter
29+
int indentSpaces = Json.DEFAULT_INDENTATION;
30+
31+
@Parameter
32+
boolean sortByKeys = false;
33+
34+
@Parameter
35+
boolean escapeHtml = false;
36+
37+
@Parameter
38+
String version = DEFAULT_GSON_VERSION;
39+
40+
@Override
41+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
42+
int indentSpaces = this.indentSpaces;
43+
return GsonStep.create(indentSpaces, sortByKeys, escapeHtml, version, stepConfig.getProvisioner());
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.json;
17+
18+
import java.util.Collections;
19+
import java.util.Set;
20+
21+
import com.diffplug.spotless.maven.FormatterFactory;
22+
23+
/**
24+
* A {@link FormatterFactory} implementation that corresponds to {@code <json>...</json>} configuration element.
25+
*/
26+
public class Json extends FormatterFactory {
27+
public static final int DEFAULT_INDENTATION = 4;
28+
29+
@Override
30+
public Set<String> defaultIncludes() {
31+
return Collections.emptySet();
32+
}
33+
34+
@Override
35+
public String licenseHeaderDelimiter() {
36+
return null;
37+
}
38+
39+
public void addSimple(Simple simple) {
40+
addStepFactory(simple);
41+
}
42+
43+
public void addGson(Gson gson) {
44+
addStepFactory(gson);
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.json;
17+
18+
import org.apache.maven.plugins.annotations.Parameter;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.json.JsonSimpleStep;
22+
import com.diffplug.spotless.maven.FormatterStepConfig;
23+
import com.diffplug.spotless.maven.FormatterStepFactory;
24+
25+
public class Simple implements FormatterStepFactory {
26+
27+
@Parameter
28+
int indentSpaces = Json.DEFAULT_INDENTATION;
29+
30+
@Override
31+
public FormatterStep newFormatterStep(FormatterStepConfig stepConfig) {
32+
int indentSpaces = this.indentSpaces;
33+
return JsonSimpleStep.create(indentSpaces, stepConfig.getProvisioner());
34+
}
35+
}

plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 DiffPlug
2+
* Copyright 2016-2023 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.
@@ -156,6 +156,10 @@ protected void writePomWithMarkdownSteps(String... steps) throws IOException {
156156
writePom(groupWithSteps("markdown", including("**/*.md"), steps));
157157
}
158158

159+
protected void writePomWithJsonSteps(String... steps) throws IOException {
160+
writePom(groupWithSteps("json", including("**/*.json"), steps));
161+
}
162+
159163
protected void writePom(String... configuration) throws IOException {
160164
writePom(null, configuration, null);
161165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2023 DiffPlug
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.diffplug.spotless.maven.json;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.maven.MavenIntegrationHarness;
21+
22+
public class JsonTest extends MavenIntegrationHarness {
23+
@Test
24+
public void testFormatJson_WithSimple_defaultConfig() throws Exception {
25+
writePomWithJsonSteps("<json><simple/></json>");
26+
27+
setFile("json_test.json").toResource("json/sortByKeysBefore.json");
28+
mavenRunner().withArguments("spotless:apply").runNoError().error();
29+
assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json");
30+
}
31+
32+
@Test
33+
public void testFormatJson_WithGson_defaultConfig() throws Exception {
34+
writePomWithJsonSteps("<json><gson/></json>");
35+
36+
setFile("json_test.json").toResource("json/sortByKeysBefore.json");
37+
mavenRunner().withArguments("spotless:apply").runNoError().error();
38+
assertFile("json_test.json").sameAsResource("json/sortByKeysAfterDisabled.json");
39+
}
40+
}

0 commit comments

Comments
 (0)