Skip to content

Commit

Permalink
Handle JSON with Array as root (#1585)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Feb 26, 2023
2 parents 3c1306b + e2f7f76 commit 2872741
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <a href="https://github.com/FasterXML/jackson-dataformats-text">jackson-dataformats-text</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonNode> 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
Expand Down
7 changes: 4 additions & 3 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (

## [Unreleased]
### Added
* Add `includeDraft` option, to include draft mutators from composite mutators ([#1574](https://github.com/diffplug/spotless/pull/1574))
* `cleanthat` now has `includeDraft` option, to include draft mutators from composite mutators ([#1574](https://github.com/diffplug/spotless/pull/1574))
### 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.2` -> `2.6` ([#1574](https://github.com/diffplug/spotless/pull/1574))
* Bump default `cleanthat` version to latest `2.1` -> `2.2` ([#1569](https://github.com/diffplug/spotless/pull/1569))
* 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))

## [6.15.0] - 2023-02-10
### Added
Expand Down
7 changes: 4 additions & 3 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `1.27.0`).

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

## [2.33.0] - 2023-02-10
### Added
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ 1, 2, 3, 4 ]
2 changes: 1 addition & 1 deletion testlib/src/main/resources/json/singletonArrayBefore.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[ 1, 2, 3, 4 ]
[ 1 , 2, 3, 4 ]
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 2872741

Please sign in to comment.