Fixed serialisation of cropId and startDate
This commit is contained in:
parent
72b0d029d5
commit
629e64143b
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
]
|
Loading…
Reference in New Issue