Skip to content

Commit

Permalink
BAEL-7944 - Sort JSON Object in Java (eugenp#17091)
Browse files Browse the repository at this point in the history
* 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
JonCook and jcook02 authored Jul 19, 2024
1 parent 1ab0ad8 commit 7289258
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 1 deletion.
2 changes: 2 additions & 0 deletions json-modules/json-operations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
## Relevant Articles

33 changes: 33 additions & 0 deletions json-modules/json-operations/pom.xml
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>
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;
}

}
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;
}
}
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 json-modules/json-operations/src/test/resources/solar_events.json
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
}
]
}
3 changes: 2 additions & 1 deletion json-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<module>json-2</module>
<module>json-arrays</module>
<module>json-conversion</module>
<module>json-operations</module>
<module>json-path</module>
<module>gson</module>
<module>gson-2</module>
Expand All @@ -38,4 +39,4 @@
<json.version>20240303</json.version>
</properties>

</project>
</project>

0 comments on commit 7289258

Please sign in to comment.