Skip to content

Commit 7289258

Browse files
JonCookjcook02
andauthored
BAEL-7944 - Sort JSON Object in Java (eugenp#17091)
* BAEL-7864 - How to convert org.w3c.dom.Document to String in Java * BAEL-7944 - Sort JSON Object in Java --------- Co-authored-by: jcook02 <jonathan.paul.cook@ext.esa.int>
1 parent 1ab0ad8 commit 7289258

File tree

7 files changed

+279
-1
lines changed

7 files changed

+279
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## Relevant Articles
2+

json-modules/json-operations/pom.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<groupId>org.baeldung</groupId>
7+
<artifactId>json-operations</artifactId>
8+
<name>json-operations</name>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>json-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.google.code.gson</groupId>
19+
<artifactId>gson</artifactId>
20+
<version>${gson.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.fasterxml.jackson.core</groupId>
24+
<artifactId>jackson-databind</artifactId>
25+
<version>${jackson.version}</version>
26+
</dependency>
27+
</dependencies>
28+
29+
<properties>
30+
<gson.version>2.11.0</gson.version>
31+
</properties>
32+
33+
</project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package com.baeldung.sorting;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
class SolarEvent {
6+
7+
@JsonProperty("event_name")
8+
private String eventName;
9+
@JsonProperty("date")
10+
private String date;
11+
@JsonProperty("coordinates")
12+
private Coordinates coordinates;
13+
@JsonProperty("type")
14+
private String type;
15+
@JsonProperty("class")
16+
private String eventClass;
17+
@JsonProperty("size")
18+
private String size;
19+
@JsonProperty("speed_km_per_s")
20+
private int speedKmPerS;
21+
22+
public String getEventName() {
23+
return eventName;
24+
}
25+
26+
public void setEventName(String eventName) {
27+
this.eventName = eventName;
28+
}
29+
30+
public String getDate() {
31+
return date;
32+
}
33+
34+
public void setDate(String date) {
35+
this.date = date;
36+
}
37+
38+
public Coordinates getCoordinates() {
39+
return coordinates;
40+
}
41+
42+
public void setCoordinates(Coordinates coordinates) {
43+
this.coordinates = coordinates;
44+
}
45+
46+
public String getType() {
47+
return type;
48+
}
49+
50+
public void setType(String type) {
51+
this.type = type;
52+
}
53+
54+
public String getEventClass() {
55+
return eventClass;
56+
}
57+
58+
public void setEventClass(String eventClass) {
59+
this.eventClass = eventClass;
60+
}
61+
62+
public String getSize() {
63+
return size;
64+
}
65+
66+
public void setSize(String size) {
67+
this.size = size;
68+
}
69+
70+
public int getSpeedKmPerS() {
71+
return speedKmPerS;
72+
}
73+
74+
public void setSpeedKmPerS(int speedKmPerS) {
75+
this.speedKmPerS = speedKmPerS;
76+
}
77+
}
78+
79+
class Coordinates {
80+
81+
@JsonProperty("latitude")
82+
private double latitude;
83+
84+
@JsonProperty("longitude")
85+
private double longitude;
86+
87+
public double getLatitude() {
88+
return latitude;
89+
}
90+
91+
public void setLatitude(double latitude) {
92+
this.latitude = latitude;
93+
}
94+
95+
public double getLongitude() {
96+
return longitude;
97+
}
98+
99+
public void setLongitude(double longitude) {
100+
this.longitude = longitude;
101+
}
102+
103+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.sorting;
2+
3+
import java.util.List;
4+
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
7+
public class SolarEventContainer {
8+
9+
@JsonProperty("solar_events")
10+
private List<SolarEvent> solarEvents;
11+
12+
// Getters and setters
13+
public List<SolarEvent> getSolarEvents() {
14+
return solarEvents;
15+
}
16+
17+
public void setSolarEvents(List<SolarEvent> solarEvents) {
18+
this.solarEvents = solarEvents;
19+
}
20+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.baeldung.sorting;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import java.io.File;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.io.IOException;
10+
import java.util.Collections;
11+
import java.util.Comparator;
12+
import java.util.List;
13+
14+
import org.junit.jupiter.api.Test;
15+
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.google.gson.JsonArray;
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonParser;
20+
import com.google.gson.stream.JsonReader;
21+
22+
class JsonObjectSortingUnitTest {
23+
24+
@Test
25+
void givenJsonObject_whenUsingJackson_thenSortedBySpeedCorrectly() throws IOException {
26+
ObjectMapper objectMapper = new ObjectMapper();
27+
SolarEventContainer container = objectMapper.readValue(
28+
new File("src/test/resources/solar_events.json"), SolarEventContainer.class);
29+
30+
List<SolarEvent> events = container.getSolarEvents();
31+
Collections.sort(events, Comparator.comparingInt(event -> event.getSpeedKmPerS()));
32+
33+
assertEquals(100, events.get(0)
34+
.getSpeedKmPerS());
35+
assertEquals(500, events.get(1)
36+
.getSpeedKmPerS());
37+
assertEquals(1000, events.get(2)
38+
.getSpeedKmPerS());
39+
assertEquals(1500, events.get(3)
40+
.getSpeedKmPerS());
41+
}
42+
43+
@Test
44+
public void givenJsonObject_whenUsingGson_thenSortedBySizeCorrectly() throws FileNotFoundException {
45+
JsonReader reader = new JsonReader(new FileReader("src/test/resources/solar_events.json"));
46+
JsonElement element = JsonParser.parseReader(reader);
47+
JsonArray events = element.getAsJsonObject().getAsJsonArray("solar_events");
48+
List<JsonElement> list = events.asList();
49+
50+
Collections.sort(list, (a, b) -> {
51+
double latA = a.getAsJsonObject()
52+
.getAsJsonObject("coordinates")
53+
.get("latitude")
54+
.getAsDouble();
55+
double latB = b.getAsJsonObject()
56+
.getAsJsonObject("coordinates")
57+
.get("latitude")
58+
.getAsDouble();
59+
return Double.compare(latA, latB);
60+
});
61+
62+
assertEquals(-5, getJsonAttributeAsInt(list.get(0)));
63+
assertEquals(0, getJsonAttributeAsInt(list.get(1)));
64+
assertEquals(15, getJsonAttributeAsInt(list.get(2)));
65+
assertEquals(37, getJsonAttributeAsInt(list.get(3)));
66+
}
67+
68+
private int getJsonAttributeAsInt(JsonElement element) {
69+
return element.getAsJsonObject()
70+
.getAsJsonObject("coordinates")
71+
.get("latitude")
72+
.getAsInt();
73+
}
74+
75+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"solar_events": [
3+
{
4+
"event_name": "Solar Eclipse",
5+
"date": "2024-04-08",
6+
"coordinates": {
7+
"latitude": 37.7749,
8+
"longitude": -122.4194
9+
},
10+
"size": "Large",
11+
"speed_km_per_s": 1000
12+
},
13+
{
14+
"event_name": "Solar Flare",
15+
"date": "2023-10-28",
16+
"coordinates": {
17+
"latitude": 0,
18+
"longitude": 0
19+
},
20+
"size": "Small",
21+
"speed_km_per_s": 100
22+
},
23+
{
24+
"event_name": "Sunspot",
25+
"date": "2023-11-15",
26+
"coordinates": {
27+
"latitude": 15,
28+
"longitude": -75
29+
},
30+
"size": "Large",
31+
"speed_km_per_s": 1500
32+
},
33+
{
34+
"event_name": "Coronal Mass Ejection",
35+
"date": "2024-01-10",
36+
"coordinates": {
37+
"latitude": -5,
38+
"longitude": 80
39+
},
40+
"size": "Medium",
41+
"speed_km_per_s": 500
42+
}
43+
]
44+
}

json-modules/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<module>json-2</module>
1919
<module>json-arrays</module>
2020
<module>json-conversion</module>
21+
<module>json-operations</module>
2122
<module>json-path</module>
2223
<module>gson</module>
2324
<module>gson-2</module>
@@ -38,4 +39,4 @@
3839
<json.version>20240303</json.version>
3940
</properties>
4041

41-
</project>
42+
</project>

0 commit comments

Comments
 (0)