diff --git a/CHANGES.md b/CHANGES.md index 401998b233..72a4b99e93 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Changes * **POTENTIALLY BREAKING** Bump bytecode from Java 8 to 11 ([#1530](https://github.com/diffplug/spotless/pull/1530) part 2 of [#1337](https://github.com/diffplug/spotless/issues/1337)) +* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546)) ## [2.34.0] - 2023-01-26 ### Added diff --git a/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java b/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java index b19476a1a8..0ff5037cba 100644 --- a/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java +++ b/lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java @@ -21,6 +21,7 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.stream.JsonWriter; @@ -57,8 +58,8 @@ public String apply(String inputString) { if (jsonElement == null) { throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE); } - if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) { - jsonElement = sortByKeys(jsonElement.getAsJsonObject()); + if (gsonConfig.isSortByKeys()) { + jsonElement = sortByKeys(jsonElement); } try (StringWriter stringWriter = new StringWriter()) { JsonWriter jsonWriter = new JsonWriter(stringWriter); @@ -72,19 +73,36 @@ public String apply(String inputString) { return result; } + private JsonElement sortByKeys(JsonElement jsonElement) { + if (jsonElement.isJsonArray()) { + return sortByKeys(jsonElement.getAsJsonArray()); + } else if (jsonElement.isJsonObject()) { + return sortByKeys(jsonElement.getAsJsonObject()); + } else { + return jsonElement; + } + } + private JsonElement sortByKeys(JsonObject jsonObject) { JsonObject result = new JsonObject(); jsonObject.keySet().stream().sorted() .forEach(key -> { - JsonElement element = jsonObject.get(key); - if (element.isJsonObject()) { - element = sortByKeys(element.getAsJsonObject()); - } - result.add(key, element); + JsonElement sorted = sortByKeys(jsonObject.get(key)); + result.add(key, sorted); }); return result; } + private JsonElement sortByKeys(JsonArray jsonArray) { + var result = new JsonArray(); + for (JsonElement element : jsonArray) { + JsonElement sorted = sortByKeys(element); + result.add(sorted); + } + + return result; + } + private String generateIndent(int indentSpaces) { return String.join("", Collections.nCopies(indentSpaces, " ")); } diff --git a/plugin-gradle/CHANGES.md b/plugin-gradle/CHANGES.md index 49ef0adbbe..a81ee7bd93 100644 --- a/plugin-gradle/CHANGES.md +++ b/plugin-gradle/CHANGES.md @@ -3,6 +3,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`). ## [Unreleased] +### Changes +* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546)) ## [6.14.0] - 2023-01-26 ### Added diff --git a/plugin-maven/CHANGES.md b/plugin-maven/CHANGES.md index 0029814ae9..9c0b237ea3 100644 --- a/plugin-maven/CHANGES.md +++ b/plugin-maven/CHANGES.md @@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format ( ## [Unreleased] ### Added * A synthesis log with the number of considered files is added after each formatter execution ([#1507](https://github.com/diffplug/spotless/pull/1507)) +### Changes +* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546)) * Any commit of the Spotless maven plugin now available via JitPack ([#1547](https://github.com/diffplug/spotless/pull/1547)) ## [2.31.0] - 2023-01-26 diff --git a/testlib/src/main/resources/json/sortByKeysAfter.json b/testlib/src/main/resources/json/sortByKeysAfter.json index c4a48de2f2..070904e872 100644 --- a/testlib/src/main/resources/json/sortByKeysAfter.json +++ b/testlib/src/main/resources/json/sortByKeysAfter.json @@ -6,6 +6,12 @@ 2, 1 ], + "_objectsInArraysAreSorted": [ + { + "a": 1, + "b": 2 + } + ], "a": 3, "c": 4, "x": 5, diff --git a/testlib/src/main/resources/json/sortByKeysAfterDisabled.json b/testlib/src/main/resources/json/sortByKeysAfterDisabled.json index de7462bb98..eb9e38241f 100644 --- a/testlib/src/main/resources/json/sortByKeysAfterDisabled.json +++ b/testlib/src/main/resources/json/sortByKeysAfterDisabled.json @@ -15,5 +15,11 @@ 3, 2, 1 + ], + "_objectsInArraysAreSorted": [ + { + "b": 2, + "a": 1 + } ] } diff --git a/testlib/src/main/resources/json/sortByKeysAfterDisabled_Simple.json b/testlib/src/main/resources/json/sortByKeysAfterDisabled_Simple.json index d2d3612fbd..cd7ebc2be1 100644 --- a/testlib/src/main/resources/json/sortByKeysAfterDisabled_Simple.json +++ b/testlib/src/main/resources/json/sortByKeysAfterDisabled_Simple.json @@ -11,6 +11,10 @@ "x": 5, "X": 2 }, + "_objectsInArraysAreSorted": [{ + "a": 1, + "b": 2 + }], "_arraysNotSorted": [ 3, 2, diff --git a/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json b/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json index 3af39fd0fb..003b2ba66f 100644 --- a/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json +++ b/testlib/src/main/resources/json/sortByKeysAfter_Jackson.json @@ -2,6 +2,10 @@ "A": 1, "X": 2, "_arraysNotSorted": [ 3, 2, 1 ], + "_objectsInArraysAreSorted": [ { + "a": 1, + "b": 2 + } ], "a": 3, "c": 4, "x": 5, diff --git a/testlib/src/main/resources/json/sortByKeysAfter_Jackson_spaceAfterKeySeparator.json b/testlib/src/main/resources/json/sortByKeysAfter_Jackson_spaceAfterKeySeparator.json index 3af39fd0fb..003b2ba66f 100644 --- a/testlib/src/main/resources/json/sortByKeysAfter_Jackson_spaceAfterKeySeparator.json +++ b/testlib/src/main/resources/json/sortByKeysAfter_Jackson_spaceAfterKeySeparator.json @@ -2,6 +2,10 @@ "A": 1, "X": 2, "_arraysNotSorted": [ 3, 2, 1 ], + "_objectsInArraysAreSorted": [ { + "a": 1, + "b": 2 + } ], "a": 3, "c": 4, "x": 5, diff --git a/testlib/src/main/resources/json/sortByKeysBefore.json b/testlib/src/main/resources/json/sortByKeysBefore.json index de7462bb98..eb9e38241f 100644 --- a/testlib/src/main/resources/json/sortByKeysBefore.json +++ b/testlib/src/main/resources/json/sortByKeysBefore.json @@ -15,5 +15,11 @@ 3, 2, 1 + ], + "_objectsInArraysAreSorted": [ + { + "b": 2, + "a": 1 + } ] }