Skip to content

Commit

Permalink
Gson sort by keys inside json arrays (#1546)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Feb 1, 2023
2 parents 7aebb7c + df8fbf2 commit fd40aa7
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 7 deletions.
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
}
]
}

0 comments on commit fd40aa7

Please sign in to comment.