diff --git a/build.gradle b/build.gradle index 122bba0..6f8ef6d 100644 --- a/build.gradle +++ b/build.gradle @@ -39,6 +39,7 @@ dependencies { testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4' implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4' + implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4' } test { diff --git a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java index c4e224c..5a25a0a 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/io/JsonGardenPlan.java @@ -2,8 +2,10 @@ package ch.zhaw.gartenverwaltung.io; import ch.zhaw.gartenverwaltung.types.Crop; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; +import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; import java.io.File; import java.io.IOException; @@ -29,9 +31,12 @@ public class JsonGardenPlan implements GardenPlan { private final static JavaTimeModule timeModule = new JavaTimeModule(); private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!"; static { - DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd"); + DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd"); LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); + LocalDateSerializer dateSerializer = new LocalDateSerializer(dateFormat); + timeModule.addDeserializer(LocalDate.class, dateDeserializer); + timeModule.addSerializer(LocalDate.class, dateSerializer); } /** @@ -123,6 +128,7 @@ public class JsonGardenPlan implements GardenPlan { try { new ObjectMapper() .registerModule(timeModule) + .registerModule(new Jdk8Module()) .writeValue(new File(dataSource.toURI()), cropMap.values()); } catch (URISyntaxException e) { // TODO: Log diff --git a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java index a55a010..fbd6286 100644 --- a/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java +++ b/src/main/java/ch/zhaw/gartenverwaltung/types/Crop.java @@ -7,7 +7,15 @@ public class Crop { private Long cropId = null; private final long plantId; private final LocalDate startDate; - private int area = 1; + private double area = 1; + + /** + * Default Constructor (needed for deserialization) + */ + public Crop() { + plantId = 0; + startDate = null; + } public Crop(long plantId, LocalDate startDate) { this.plantId = plantId; @@ -19,7 +27,7 @@ public class Crop { this.cropId = cropId; return this; } - public Crop withArea(int area) { + public Crop withArea(double area) { this.area = area; return this; } @@ -31,5 +39,14 @@ public class Crop { public long getPlantId() { return plantId; } public LocalDate getStartDate() { return startDate; } - public int getArea() { return area; } + public double getArea() { return area; } + + @Override + public String toString() { + return String.format("Crop [ cropId: %d, plantId: %d, startDate: %s, area: %f ]", + cropId, + plantId, + startDate, + area); + } } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 8e02fc7..00cf813 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -3,10 +3,10 @@ module ch.zhaw.gartenverwaltung { requires javafx.fxml; requires com.fasterxml.jackson.databind; requires com.fasterxml.jackson.datatype.jsr310; + requires com.fasterxml.jackson.datatype.jdk8; opens ch.zhaw.gartenverwaltung to javafx.fxml; opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; -// opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind; exports ch.zhaw.gartenverwaltung; exports ch.zhaw.gartenverwaltung.types; exports ch.zhaw.gartenverwaltung.json; diff --git a/src/main/resources/ch/zhaw/gartenverwaltung/io/user-crops.json b/src/main/resources/ch/zhaw/gartenverwaltung/io/user-crops.json new file mode 100644 index 0000000..ebd1d2d --- /dev/null +++ b/src/main/resources/ch/zhaw/gartenverwaltung/io/user-crops.json @@ -0,0 +1,20 @@ +[ + { + "cropId": 0, + "plantId": 1, + "startDate": "2023-02-25", + "area": 0.5 + }, + { + "cropId": 1, + "plantId": 1, + "startDate": "2023-03-01", + "area": 0.5 + }, + { + "cropId": 2, + "plantId": 0, + "startDate": "2023-03-25", + "area": 1.5 + } +] \ No newline at end of file