forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
7 changed files
with
279 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
## Relevant Articles | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>org.baeldung</groupId> | ||
<artifactId>json-operations</artifactId> | ||
<name>json-operations</name> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>json-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>${gson.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.fasterxml.jackson.core</groupId> | ||
<artifactId>jackson-databind</artifactId> | ||
<version>${jackson.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<properties> | ||
<gson.version>2.11.0</gson.version> | ||
</properties> | ||
|
||
</project> |
103 changes: 103 additions & 0 deletions
103
json-modules/json-operations/src/main/java/com/baeldung/sorting/SolarEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.baeldung.sorting; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
class SolarEvent { | ||
|
||
@JsonProperty("event_name") | ||
private String eventName; | ||
@JsonProperty("date") | ||
private String date; | ||
@JsonProperty("coordinates") | ||
private Coordinates coordinates; | ||
@JsonProperty("type") | ||
private String type; | ||
@JsonProperty("class") | ||
private String eventClass; | ||
@JsonProperty("size") | ||
private String size; | ||
@JsonProperty("speed_km_per_s") | ||
private int speedKmPerS; | ||
|
||
public String getEventName() { | ||
return eventName; | ||
} | ||
|
||
public void setEventName(String eventName) { | ||
this.eventName = eventName; | ||
} | ||
|
||
public String getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(String date) { | ||
this.date = date; | ||
} | ||
|
||
public Coordinates getCoordinates() { | ||
return coordinates; | ||
} | ||
|
||
public void setCoordinates(Coordinates coordinates) { | ||
this.coordinates = coordinates; | ||
} | ||
|
||
public String getType() { | ||
return type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = type; | ||
} | ||
|
||
public String getEventClass() { | ||
return eventClass; | ||
} | ||
|
||
public void setEventClass(String eventClass) { | ||
this.eventClass = eventClass; | ||
} | ||
|
||
public String getSize() { | ||
return size; | ||
} | ||
|
||
public void setSize(String size) { | ||
this.size = size; | ||
} | ||
|
||
public int getSpeedKmPerS() { | ||
return speedKmPerS; | ||
} | ||
|
||
public void setSpeedKmPerS(int speedKmPerS) { | ||
this.speedKmPerS = speedKmPerS; | ||
} | ||
} | ||
|
||
class Coordinates { | ||
|
||
@JsonProperty("latitude") | ||
private double latitude; | ||
|
||
@JsonProperty("longitude") | ||
private double longitude; | ||
|
||
public double getLatitude() { | ||
return latitude; | ||
} | ||
|
||
public void setLatitude(double latitude) { | ||
this.latitude = latitude; | ||
} | ||
|
||
public double getLongitude() { | ||
return longitude; | ||
} | ||
|
||
public void setLongitude(double longitude) { | ||
this.longitude = longitude; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
json-modules/json-operations/src/main/java/com/baeldung/sorting/SolarEventContainer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.baeldung.sorting; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class SolarEventContainer { | ||
|
||
@JsonProperty("solar_events") | ||
private List<SolarEvent> solarEvents; | ||
|
||
// Getters and setters | ||
public List<SolarEvent> getSolarEvents() { | ||
return solarEvents; | ||
} | ||
|
||
public void setSolarEvents(List<SolarEvent> solarEvents) { | ||
this.solarEvents = solarEvents; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...modules/json-operations/src/test/java/com/baeldung/sorting/JsonObjectSortingUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.baeldung.sorting; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonParser; | ||
import com.google.gson.stream.JsonReader; | ||
|
||
class JsonObjectSortingUnitTest { | ||
|
||
@Test | ||
void givenJsonObject_whenUsingJackson_thenSortedBySpeedCorrectly() throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
SolarEventContainer container = objectMapper.readValue( | ||
new File("src/test/resources/solar_events.json"), SolarEventContainer.class); | ||
|
||
List<SolarEvent> events = container.getSolarEvents(); | ||
Collections.sort(events, Comparator.comparingInt(event -> event.getSpeedKmPerS())); | ||
|
||
assertEquals(100, events.get(0) | ||
.getSpeedKmPerS()); | ||
assertEquals(500, events.get(1) | ||
.getSpeedKmPerS()); | ||
assertEquals(1000, events.get(2) | ||
.getSpeedKmPerS()); | ||
assertEquals(1500, events.get(3) | ||
.getSpeedKmPerS()); | ||
} | ||
|
||
@Test | ||
public void givenJsonObject_whenUsingGson_thenSortedBySizeCorrectly() throws FileNotFoundException { | ||
JsonReader reader = new JsonReader(new FileReader("src/test/resources/solar_events.json")); | ||
JsonElement element = JsonParser.parseReader(reader); | ||
JsonArray events = element.getAsJsonObject().getAsJsonArray("solar_events"); | ||
List<JsonElement> list = events.asList(); | ||
|
||
Collections.sort(list, (a, b) -> { | ||
double latA = a.getAsJsonObject() | ||
.getAsJsonObject("coordinates") | ||
.get("latitude") | ||
.getAsDouble(); | ||
double latB = b.getAsJsonObject() | ||
.getAsJsonObject("coordinates") | ||
.get("latitude") | ||
.getAsDouble(); | ||
return Double.compare(latA, latB); | ||
}); | ||
|
||
assertEquals(-5, getJsonAttributeAsInt(list.get(0))); | ||
assertEquals(0, getJsonAttributeAsInt(list.get(1))); | ||
assertEquals(15, getJsonAttributeAsInt(list.get(2))); | ||
assertEquals(37, getJsonAttributeAsInt(list.get(3))); | ||
} | ||
|
||
private int getJsonAttributeAsInt(JsonElement element) { | ||
return element.getAsJsonObject() | ||
.getAsJsonObject("coordinates") | ||
.get("latitude") | ||
.getAsInt(); | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
json-modules/json-operations/src/test/resources/solar_events.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"solar_events": [ | ||
{ | ||
"event_name": "Solar Eclipse", | ||
"date": "2024-04-08", | ||
"coordinates": { | ||
"latitude": 37.7749, | ||
"longitude": -122.4194 | ||
}, | ||
"size": "Large", | ||
"speed_km_per_s": 1000 | ||
}, | ||
{ | ||
"event_name": "Solar Flare", | ||
"date": "2023-10-28", | ||
"coordinates": { | ||
"latitude": 0, | ||
"longitude": 0 | ||
}, | ||
"size": "Small", | ||
"speed_km_per_s": 100 | ||
}, | ||
{ | ||
"event_name": "Sunspot", | ||
"date": "2023-11-15", | ||
"coordinates": { | ||
"latitude": 15, | ||
"longitude": -75 | ||
}, | ||
"size": "Large", | ||
"speed_km_per_s": 1500 | ||
}, | ||
{ | ||
"event_name": "Coronal Mass Ejection", | ||
"date": "2024-01-10", | ||
"coordinates": { | ||
"latitude": -5, | ||
"longitude": 80 | ||
}, | ||
"size": "Medium", | ||
"speed_km_per_s": 500 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters