Skip to content

Commit 2872741

Browse files
authored
Handle JSON with Array as root (#1585)
2 parents 3c1306b + e2f7f76 commit 2872741

File tree

9 files changed

+78
-10
lines changed

9 files changed

+78
-10
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This document is intended for Spotless developers.
1010
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
1111

1212
## [Unreleased]
13+
### Fixed
14+
* `JacksonJsonFormatterFunc` handles json files with an Array as root. ([#1585](https://github.com/diffplug/spotless/pull/1585))
1315

1416
## [2.35.0] - 2023-02-10
1517
### Added

lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
package com.diffplug.spotless.glue.json;
1717

1818
import java.io.IOException;
19-
import java.util.Map;
2019

2120
import com.fasterxml.jackson.core.JsonFactory;
2221
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -49,7 +48,7 @@ public String apply(String input) throws Exception {
4948
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException {
5049
try {
5150
// ObjectNode is not compatible with SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS
52-
Map objectNode = objectMapper.readValue(input, Map.class);
51+
Object objectNode = objectMapper.readValue(input, inferType(input));
5352
String output = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode);
5453

5554
return output;
@@ -58,6 +57,13 @@ protected String format(ObjectMapper objectMapper, String input) throws IllegalA
5857
}
5958
}
6059

60+
/**
61+
*
62+
* @param input
63+
* @return the {@link Class} into which the String has to be deserialized
64+
*/
65+
protected abstract Class<?> inferType(String input);
66+
6167
/**
6268
* @return a {@link JsonFactory}. May be overridden to handle alternative formats.
6369
* @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>

lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package com.diffplug.spotless.glue.json;
1717

18+
import java.util.Collection;
19+
import java.util.Map;
20+
1821
import com.fasterxml.jackson.core.JsonFactory;
1922
import com.fasterxml.jackson.core.JsonFactoryBuilder;
2023
import com.fasterxml.jackson.core.JsonGenerator;
@@ -37,6 +40,15 @@ public JacksonJsonFormatterFunc(JacksonJsonConfig jacksonConfig) {
3740
this.jacksonConfig = jacksonConfig;
3841
}
3942

43+
@Override
44+
protected Class<?> inferType(String input) {
45+
if (input.trim().startsWith("[")) {
46+
return Collection.class;
47+
} else {
48+
return Map.class;
49+
}
50+
}
51+
4052
/**
4153
* @return a {@link JsonFactory}. May be overridden to handle alternative formats.
4254
* @see <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>

lib/src/jackson/java/com/diffplug/spotless/glue/yaml/JacksonYamlFormatterFunc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,18 @@ protected JsonFactory makeJsonFactory() {
5858
return yamlFactoryBuilder.build();
5959
}
6060

61+
@Override
62+
protected Class<?> inferType(String input) {
63+
return JsonNode.class;
64+
}
65+
6166
@Override
6267
protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException {
6368
try {
6469
// https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson
6570
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-375328648
6671
JsonParser yamlParser = objectMapper.getFactory().createParser(input);
67-
List<JsonNode> documents = objectMapper.readValues(yamlParser, JsonNode.class).readAll();
72+
List<?> documents = objectMapper.readValues(yamlParser, inferType(input)).readAll();
6873

6974
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055
7075
// https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055

plugin-gradle/CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
44

55
## [Unreleased]
66
### Added
7-
* Add `includeDraft` option, to include draft mutators from composite mutators ([#1574](https://github.com/diffplug/spotless/pull/1574))
7+
* `cleanthat` now has `includeDraft` option, to include draft mutators from composite mutators ([#1574](https://github.com/diffplug/spotless/pull/1574))
8+
### Fixed
9+
* `json { jackson()` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585))
810
### Changes
9-
* Bump default `cleanthat` version to latest `2.2` -> `2.6` ([#1574](https://github.com/diffplug/spotless/pull/1574))
10-
* Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))
11+
* Bump default `cleanthat` version to latest `2.1` -> `2.6` ([#1569](https://github.com/diffplug/spotless/pull/1569) and [#1574](https://github.com/diffplug/spotless/pull/1574))
1112

1213
## [6.15.0] - 2023-02-10
1314
### Added

plugin-maven/CHANGES.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).
44

55
## [Unreleased]
6+
### Fixed
7+
* `<json><jackson>` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585))
68
### Added
7-
* Add `includeDraft` option, to include draft mutators from composite mutators ([#XXX](https://github.com/diffplug/spotless/pull/XXX))
9+
* `cleanthat` added `includeDraft` option, to include draft mutators from composite mutators ([#XXX](https://github.com/diffplug/spotless/pull/XXX))
810
### Changes
9-
* Bump default `cleanthat` version to latest `2.2` -> `2.6` ([#1574](https://github.com/diffplug/spotless/pull/1574))
10-
* Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))
11+
* Bump default `cleanthat` version to latest `2.1` -> `2.6` ([#1569](https://github.com/diffplug/spotless/pull/1569) and [#1574](https://github.com/diffplug/spotless/pull/1574))
1112

1213
## [2.33.0] - 2023-02-10
1314
### Added
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ 1, 2, 3, 4 ]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[ 1, 2, 3, 4 ]
1+
[ 1 , 2, 3, 4 ]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2021-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.json;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import com.diffplug.spotless.FormatterStep;
21+
import com.diffplug.spotless.StepHarness;
22+
import com.diffplug.spotless.TestProvisioner;
23+
24+
class JacksonJsonStepTest {
25+
26+
private static final int INDENT = 4;
27+
28+
private final FormatterStep step = JsonSimpleStep.create(INDENT, TestProvisioner.mavenCentral());
29+
private final StepHarness stepHarness = StepHarness.forStep(step);
30+
31+
@Test
32+
void canSetCustomIndentationLevel() {
33+
FormatterStep step = JacksonJsonStep.create(TestProvisioner.mavenCentral());
34+
StepHarness stepHarness = StepHarness.forStep(step);
35+
36+
String before = "json/singletonArrayBefore.json";
37+
String after = "json/singletonArrayAfter_Jackson.json";
38+
stepHarness.testResource(before, after);
39+
}
40+
}

0 commit comments

Comments
 (0)