Skip to content

Commit 3636eba

Browse files
committed
! F JsonUtils.reorderFields
1 parent c61f422 commit 3636eba

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

approvaltests-tests/src/test/java/org/approvaltests/JsonFormattingTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.approvaltests;
22

33
import com.google.gson.GsonBuilder;
4+
import com.spun.util.JsonUtils;
45
import org.junit.jupiter.api.Test;
56

67
import java.time.Instant;
@@ -57,4 +58,22 @@ private static class DateStuff
5758
{
5859
public Instant instant;
5960
}
61+
@Test
62+
public void testJsonFieldOrdering()
63+
{
64+
Approvals.settings().allowMultipleVerifyCallsForThisMethod();
65+
String json1 = "{\"infos\":{\"address\":\"my address\",\"phone\":\"my phone\"},\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]}}";
66+
String json2 = "{\"insurance\":{\"forks\":[14,53,123],\"prices\":[5,8,\"3%\"]},\"infos\":{\"phone\":\"my phone\",\"address\":\"my address\"}}";
67+
// JsonApprovals.verifyJson(json1);
68+
// JsonApprovals.verifyJson(json2);
69+
//
70+
// JsonApprovals.verifyOrderedJson(json1);
71+
// JsonApprovals.verifyOrderedJson(json2);
72+
//
73+
// JsonApprovals.verifyJson(json1, true);
74+
// JsonApprovals.verifyJson(json2, true);
75+
76+
JsonApprovals.verifyJson(JsonUtils.reorderFields(json1));
77+
JsonApprovals.verifyJson(JsonUtils.reorderFields(json2));
78+
}
6079
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"infos": {
3+
"address": "my address",
4+
"phone": "my phone"
5+
},
6+
"insurance": {
7+
"forks": [
8+
14,
9+
53,
10+
123
11+
],
12+
"prices": [
13+
5,
14+
8,
15+
"3%"
16+
]
17+
}
18+
}

approvaltests-util/src/main/java/com/spun/util/JsonUtils.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
import org.lambda.functions.Function1;
77

88
import java.io.IOException;
9+
import java.lang.reflect.Field;
910
import java.time.Instant;
1011
import java.time.LocalDateTime;
12+
import java.util.Map;
13+
import java.util.TreeMap;
14+
import java.util.stream.Collectors;
1115

1216
public class JsonUtils
1317
{
@@ -61,6 +65,34 @@ private static GsonBuilder addHandlingForDateObjects(GsonBuilder builder)
6165
builder = builder.registerTypeAdapter(LocalDateTime.class, new LocalDateTimeAdapter());
6266
return builder;
6367
}
68+
69+
public static String reorderFields(String json) {
70+
JsonObject sortedJsonObject = sortJsonObject(json);
71+
return asJson(sortedJsonObject);
72+
}
73+
74+
public static JsonObject sortJsonObject(String json) {
75+
JsonObject jsonObject = JsonParser.parseString(json).getAsJsonObject();
76+
return sortJsonObjectFields(jsonObject);
77+
}
78+
79+
public static JsonObject sortJsonObjectFields(JsonObject jsonObject) {
80+
JsonObject sortedJsonObject = new JsonObject();
81+
Map<String, JsonElement> sortedFirstLevelFields = jsonObject.entrySet().stream()
82+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, TreeMap::new));
83+
84+
for (Map.Entry<String, JsonElement> entry : sortedFirstLevelFields.entrySet()) {
85+
JsonElement element = entry.getValue();
86+
if (element.isJsonObject()) {
87+
sortedJsonObject.add(entry.getKey(), sortJsonObjectFields(element.getAsJsonObject()));
88+
} else {
89+
sortedJsonObject.add(entry.getKey(), element);
90+
}
91+
}
92+
93+
return sortedJsonObject;
94+
}
95+
6496
public static class InstantAdapter extends TypeAdapter<Instant>
6597
{
6698
@Override

0 commit comments

Comments
 (0)