Fixed serialisation of cropId and startDate

This commit is contained in:
David Guler 2022-10-26 20:32:01 +02:00
parent 72b0d029d5
commit 629e64143b
5 changed files with 49 additions and 5 deletions

View File

@ -39,6 +39,7 @@ dependencies {
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${junitVersion}")
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4' 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-jsr310:2.13.4'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.13.4'
} }
test { test {

View File

@ -2,8 +2,10 @@ package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.Crop; import ch.zhaw.gartenverwaltung.types.Crop;
import com.fasterxml.jackson.databind.ObjectMapper; 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.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer; import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@ -29,9 +31,12 @@ public class JsonGardenPlan implements GardenPlan {
private final static JavaTimeModule timeModule = new JavaTimeModule(); private final static JavaTimeModule timeModule = new JavaTimeModule();
private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!"; private final static String INVALID_DATASOURCE_MSG = "Invalid datasource specified!";
static { static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yy-MM-dd"); DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat); LocalDateDeserializer dateDeserializer = new LocalDateDeserializer(dateFormat);
LocalDateSerializer dateSerializer = new LocalDateSerializer(dateFormat);
timeModule.addDeserializer(LocalDate.class, dateDeserializer); timeModule.addDeserializer(LocalDate.class, dateDeserializer);
timeModule.addSerializer(LocalDate.class, dateSerializer);
} }
/** /**
@ -123,6 +128,7 @@ public class JsonGardenPlan implements GardenPlan {
try { try {
new ObjectMapper() new ObjectMapper()
.registerModule(timeModule) .registerModule(timeModule)
.registerModule(new Jdk8Module())
.writeValue(new File(dataSource.toURI()), cropMap.values()); .writeValue(new File(dataSource.toURI()), cropMap.values());
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
// TODO: Log // TODO: Log

View File

@ -7,7 +7,15 @@ public class Crop {
private Long cropId = null; private Long cropId = null;
private final long plantId; private final long plantId;
private final LocalDate startDate; 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) { public Crop(long plantId, LocalDate startDate) {
this.plantId = plantId; this.plantId = plantId;
@ -19,7 +27,7 @@ public class Crop {
this.cropId = cropId; this.cropId = cropId;
return this; return this;
} }
public Crop withArea(int area) { public Crop withArea(double area) {
this.area = area; this.area = area;
return this; return this;
} }
@ -31,5 +39,14 @@ public class Crop {
public long getPlantId() { return plantId; } public long getPlantId() { return plantId; }
public LocalDate getStartDate() { return startDate; } 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);
}
} }

View File

@ -3,10 +3,10 @@ module ch.zhaw.gartenverwaltung {
requires javafx.fxml; requires javafx.fxml;
requires com.fasterxml.jackson.databind; requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.datatype.jsr310; requires com.fasterxml.jackson.datatype.jsr310;
requires com.fasterxml.jackson.datatype.jdk8;
opens ch.zhaw.gartenverwaltung to javafx.fxml; 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;
// opens ch.zhaw.gartenverwaltung.types to com.fasterxml.jackson.databind;
exports ch.zhaw.gartenverwaltung; exports ch.zhaw.gartenverwaltung;
exports ch.zhaw.gartenverwaltung.types; exports ch.zhaw.gartenverwaltung.types;
exports ch.zhaw.gartenverwaltung.json; exports ch.zhaw.gartenverwaltung.json;

View File

@ -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
}
]