Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort by keys inside json arrays #1546

Merged
merged 6 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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, " "));
}
Expand Down
2 changes: 2 additions & 0 deletions plugin-gradle/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions plugin-maven/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions testlib/src/main/resources/json/sortByKeysAfter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
2,
1
],
"_objectsInArraysAreSorted": [
{
"a": 1,
"b": 2
}
],
"a": 3,
"c": 4,
"x": 5,
Expand Down
6 changes: 6 additions & 0 deletions testlib/src/main/resources/json/sortByKeysAfterDisabled.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
3,
2,
1
],
"_objectsInArraysAreSorted": [
{
"b": 2,
"a": 1
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
"x": 5,
"X": 2
},
"_objectsInArraysAreSorted": [{
"a": 1,
"b": 2
}],
"_arraysNotSorted": [
3,
2,
Expand Down
4 changes: 4 additions & 0 deletions testlib/src/main/resources/json/sortByKeysAfter_Jackson.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"A": 1,
"X": 2,
"_arraysNotSorted": [ 3, 2, 1 ],
"_objectsInArraysAreSorted": [ {
"a": 1,
"b": 2
} ],
"a": 3,
"c": 4,
"x": 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"A": 1,
"X": 2,
"_arraysNotSorted": [ 3, 2, 1 ],
"_objectsInArraysAreSorted": [ {
"a": 1,
"b": 2
} ],
"a": 3,
"c": 4,
"x": 5,
Expand Down
6 changes: 6 additions & 0 deletions testlib/src/main/resources/json/sortByKeysBefore.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
3,
2,
1
],
"_objectsInArraysAreSorted": [
{
"b": 2,
"a": 1
}
]
}