Skip to content

Commit fd40aa7

Browse files
authored
Gson sort by keys inside json arrays (#1546)
2 parents 7aebb7c + df8fbf2 commit fd40aa7

10 files changed

+60
-7
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
1212
## [Unreleased]
1313
### Changes
1414
* **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))
15+
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
1516

1617
## [2.34.0] - 2023-01-26
1718
### Added

lib/src/gson/java/com/diffplug/spotless/glue/gson/GsonFormatterFunc.java

+25-7
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.google.gson.Gson;
2323
import com.google.gson.GsonBuilder;
24+
import com.google.gson.JsonArray;
2425
import com.google.gson.JsonElement;
2526
import com.google.gson.JsonObject;
2627
import com.google.gson.stream.JsonWriter;
@@ -57,8 +58,8 @@ public String apply(String inputString) {
5758
if (jsonElement == null) {
5859
throw new AssertionError(FAILED_TO_PARSE_ERROR_MESSAGE);
5960
}
60-
if (gsonConfig.isSortByKeys() && jsonElement.isJsonObject()) {
61-
jsonElement = sortByKeys(jsonElement.getAsJsonObject());
61+
if (gsonConfig.isSortByKeys()) {
62+
jsonElement = sortByKeys(jsonElement);
6263
}
6364
try (StringWriter stringWriter = new StringWriter()) {
6465
JsonWriter jsonWriter = new JsonWriter(stringWriter);
@@ -72,19 +73,36 @@ public String apply(String inputString) {
7273
return result;
7374
}
7475

76+
private JsonElement sortByKeys(JsonElement jsonElement) {
77+
if (jsonElement.isJsonArray()) {
78+
return sortByKeys(jsonElement.getAsJsonArray());
79+
} else if (jsonElement.isJsonObject()) {
80+
return sortByKeys(jsonElement.getAsJsonObject());
81+
} else {
82+
return jsonElement;
83+
}
84+
}
85+
7586
private JsonElement sortByKeys(JsonObject jsonObject) {
7687
JsonObject result = new JsonObject();
7788
jsonObject.keySet().stream().sorted()
7889
.forEach(key -> {
79-
JsonElement element = jsonObject.get(key);
80-
if (element.isJsonObject()) {
81-
element = sortByKeys(element.getAsJsonObject());
82-
}
83-
result.add(key, element);
90+
JsonElement sorted = sortByKeys(jsonObject.get(key));
91+
result.add(key, sorted);
8492
});
8593
return result;
8694
}
8795

96+
private JsonElement sortByKeys(JsonArray jsonArray) {
97+
var result = new JsonArray();
98+
for (JsonElement element : jsonArray) {
99+
JsonElement sorted = sortByKeys(element);
100+
result.add(sorted);
101+
}
102+
103+
return result;
104+
}
105+
88106
private String generateIndent(int indentSpaces) {
89107
return String.join("", Collections.nCopies(indentSpaces, " "));
90108
}

plugin-gradle/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (starting after version `3.27.0`).
44

55
## [Unreleased]
6+
### Changes
7+
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
68

79
## [6.14.0] - 2023-01-26
810
### Added

plugin-maven/CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ We adhere to the [keepachangelog](https://keepachangelog.com/en/1.0.0/) format (
55
## [Unreleased]
66
### Added
77
* A synthesis log with the number of considered files is added after each formatter execution ([#1507](https://github.com/diffplug/spotless/pull/1507))
8+
### Changes
9+
* **POTENTIALLY BREAKING** `sortByKeys` for JSON formatting now takes into account objects inside arrays ([#1546](https://github.com/diffplug/spotless/pull/1546))
810
* Any commit of the Spotless maven plugin now available via JitPack ([#1547](https://github.com/diffplug/spotless/pull/1547))
911

1012
## [2.31.0] - 2023-01-26

testlib/src/main/resources/json/sortByKeysAfter.json

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
2,
77
1
88
],
9+
"_objectsInArraysAreSorted": [
10+
{
11+
"a": 1,
12+
"b": 2
13+
}
14+
],
915
"a": 3,
1016
"c": 4,
1117
"x": 5,

testlib/src/main/resources/json/sortByKeysAfterDisabled.json

+6
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,11 @@
1515
3,
1616
2,
1717
1
18+
],
19+
"_objectsInArraysAreSorted": [
20+
{
21+
"b": 2,
22+
"a": 1
23+
}
1824
]
1925
}

testlib/src/main/resources/json/sortByKeysAfterDisabled_Simple.json

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
"x": 5,
1212
"X": 2
1313
},
14+
"_objectsInArraysAreSorted": [{
15+
"a": 1,
16+
"b": 2
17+
}],
1418
"_arraysNotSorted": [
1519
3,
1620
2,

testlib/src/main/resources/json/sortByKeysAfter_Jackson.json

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"A": 1,
33
"X": 2,
44
"_arraysNotSorted": [ 3, 2, 1 ],
5+
"_objectsInArraysAreSorted": [ {
6+
"a": 1,
7+
"b": 2
8+
} ],
59
"a": 3,
610
"c": 4,
711
"x": 5,

testlib/src/main/resources/json/sortByKeysAfter_Jackson_spaceAfterKeySeparator.json

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"A": 1,
33
"X": 2,
44
"_arraysNotSorted": [ 3, 2, 1 ],
5+
"_objectsInArraysAreSorted": [ {
6+
"a": 1,
7+
"b": 2
8+
} ],
59
"a": 3,
610
"c": 4,
711
"x": 5,

testlib/src/main/resources/json/sortByKeysBefore.json

+6
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,11 @@
1515
3,
1616
2,
1717
1
18+
],
19+
"_objectsInArraysAreSorted": [
20+
{
21+
"b": 2,
22+
"a": 1
23+
}
1824
]
1925
}

0 commit comments

Comments
 (0)