From 1557a0b6599efbd0ad246d19c149d59dbfcdc508 Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 24 Feb 2023 13:23:29 +0400 Subject: [PATCH 1/3] Handle JSON with Array as root --- .../glue/json/AJacksonFormatterFunc.java | 10 ++++- .../glue/json/JacksonJsonFormatterFunc.java | 12 ++++++ .../glue/yaml/JacksonYamlFormatterFunc.java | 7 +++- .../json/singletonArrayAfter_Jackson.json | 1 + .../resources/json/singletonArrayBefore.json | 2 +- .../spotless/json/JacksonJsonStepTest.java | 40 +++++++++++++++++++ 6 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 testlib/src/main/resources/json/singletonArrayAfter_Jackson.json create mode 100644 testlib/src/test/java/com/diffplug/spotless/json/JacksonJsonStepTest.java diff --git a/lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.java b/lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.java index 6f363ad1b7..1f317fa3b4 100644 --- a/lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.java +++ b/lib/src/jackson/java/com/diffplug/spotless/glue/json/AJacksonFormatterFunc.java @@ -16,7 +16,6 @@ package com.diffplug.spotless.glue.json; import java.io.IOException; -import java.util.Map; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonProcessingException; @@ -49,7 +48,7 @@ public String apply(String input) throws Exception { protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException { try { // ObjectNode is not compatible with SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS - Map objectNode = objectMapper.readValue(input, Map.class); + Object objectNode = objectMapper.readValue(input, inferType(input)); String output = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode); return output; @@ -58,6 +57,13 @@ protected String format(ObjectMapper objectMapper, String input) throws IllegalA } } + /** + * + * @param input + * @return the {@link Class} into which the String has to be deserialized + */ + protected abstract Class inferType(String input); + /** * @return a {@link JsonFactory}. May be overridden to handle alternative formats. * @see jackson-dataformats-text diff --git a/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java b/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java index ae455fbbb4..71215eb4a3 100644 --- a/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java +++ b/lib/src/jackson/java/com/diffplug/spotless/glue/json/JacksonJsonFormatterFunc.java @@ -15,6 +15,9 @@ */ package com.diffplug.spotless.glue.json; +import java.util.Collection; +import java.util.Map; + import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactoryBuilder; import com.fasterxml.jackson.core.JsonGenerator; @@ -37,6 +40,15 @@ public JacksonJsonFormatterFunc(JacksonJsonConfig jacksonConfig) { this.jacksonConfig = jacksonConfig; } + @Override + protected Class inferType(String input) { + if (input.trim().startsWith("[")) { + return Collection.class; + } else { + return Map.class; + } + } + /** * @return a {@link JsonFactory}. May be overridden to handle alternative formats. * @see jackson-dataformats-text diff --git a/lib/src/jackson/java/com/diffplug/spotless/glue/yaml/JacksonYamlFormatterFunc.java b/lib/src/jackson/java/com/diffplug/spotless/glue/yaml/JacksonYamlFormatterFunc.java index 89304d8d0a..b4f8ca6aee 100644 --- a/lib/src/jackson/java/com/diffplug/spotless/glue/yaml/JacksonYamlFormatterFunc.java +++ b/lib/src/jackson/java/com/diffplug/spotless/glue/yaml/JacksonYamlFormatterFunc.java @@ -58,13 +58,18 @@ protected JsonFactory makeJsonFactory() { return yamlFactoryBuilder.build(); } + @Override + protected Class inferType(String input) { + return JsonNode.class; + } + @Override protected String format(ObjectMapper objectMapper, String input) throws IllegalArgumentException, IOException { try { // https://stackoverflow.com/questions/25222327/deserialize-pojos-from-multiple-yaml-documents-in-a-single-file-in-jackson // https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-375328648 JsonParser yamlParser = objectMapper.getFactory().createParser(input); - List documents = objectMapper.readValues(yamlParser, JsonNode.class).readAll(); + List documents = objectMapper.readValues(yamlParser, inferType(input)).readAll(); // https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055 // https://github.com/FasterXML/jackson-dataformats-text/issues/66#issuecomment-554265055 diff --git a/testlib/src/main/resources/json/singletonArrayAfter_Jackson.json b/testlib/src/main/resources/json/singletonArrayAfter_Jackson.json new file mode 100644 index 0000000000..243bc2550b --- /dev/null +++ b/testlib/src/main/resources/json/singletonArrayAfter_Jackson.json @@ -0,0 +1 @@ +[ 1, 2, 3, 4 ] \ No newline at end of file diff --git a/testlib/src/main/resources/json/singletonArrayBefore.json b/testlib/src/main/resources/json/singletonArrayBefore.json index 8290d39198..18d09f95fe 100644 --- a/testlib/src/main/resources/json/singletonArrayBefore.json +++ b/testlib/src/main/resources/json/singletonArrayBefore.json @@ -1 +1 @@ -[ 1, 2, 3, 4 ] +[ 1 , 2, 3, 4 ] diff --git a/testlib/src/test/java/com/diffplug/spotless/json/JacksonJsonStepTest.java b/testlib/src/test/java/com/diffplug/spotless/json/JacksonJsonStepTest.java new file mode 100644 index 0000000000..be681653e1 --- /dev/null +++ b/testlib/src/test/java/com/diffplug/spotless/json/JacksonJsonStepTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2021-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.json; + +import org.junit.jupiter.api.Test; + +import com.diffplug.spotless.FormatterStep; +import com.diffplug.spotless.StepHarness; +import com.diffplug.spotless.TestProvisioner; + +class JacksonJsonStepTest { + + private static final int INDENT = 4; + + private final FormatterStep step = JsonSimpleStep.create(INDENT, TestProvisioner.mavenCentral()); + private final StepHarness stepHarness = StepHarness.forStep(step); + + @Test + void canSetCustomIndentationLevel() { + FormatterStep step = JacksonJsonStep.create(TestProvisioner.mavenCentral()); + StepHarness stepHarness = StepHarness.forStep(step); + + String before = "json/singletonArrayBefore.json"; + String after = "json/singletonArrayAfter_Jackson.json"; + stepHarness.testResource(before, after); + } +} From d7129f64664be1f25014bc926445ec4e1c84baed Mon Sep 17 00:00:00 2001 From: Benoit Lacelle Date: Fri, 24 Feb 2023 13:27:59 +0400 Subject: [PATCH 2/3] Add entry in CHANGES --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index eec8753271..efb88d972d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,8 @@ This document is intended for Spotless developers. We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* `JacksonJsonFormatterFunc` handles json files with an Array as root. ([#1585](https://github.com/diffplug/spotless/pull/1585)) ## [2.35.0] - 2023-02-10 ### Added From c7e46d84cc7e81134d1b1df258ca9a51fd328dfb Mon Sep 17 00:00:00 2001 From: Ned Twigg Date: Sun, 26 Feb 2023 12:20:31 -0800 Subject: [PATCH 3/3] Update plugin changelogs. --- plugin-gradle/CHANGES.md | 3 +++ plugin-maven/CHANGES.md | 2 ++ 2 files changed, 5 insertions(+) diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index afba5abdc7..7d70a74149 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,9 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Fixed +* `json { jackson()` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585)) +### Changes * Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569)) ## [6.15.0] - 2023-02-10 diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 8d4c67a9b6..e3ff459b33 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`). ## [Unreleased] +### Fixed +* `` can now handle `Array` as a root element. ([#1585](https://github.com/diffplug/spotless/pull/1585)) ### Changes * Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))