Minimum viable deserialization

Added TaskTemplate type
Added dummy data with (probably) usable format
Used the java.time classes instead of the legacy util.Date
This commit is contained in:
David Guler
2022-10-20 21:46:00 +02:00
parent 5f53bb86c6
commit 6d13bede7a
9 changed files with 173 additions and 43 deletions
@@ -3,11 +3,13 @@ package ch.zhaw.gartenverwaltung.io;
import ch.zhaw.gartenverwaltung.types.HardinessZone;
import ch.zhaw.gartenverwaltung.types.Plant;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
import java.io.IOException;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.MonthDay;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -15,14 +17,20 @@ import java.util.Optional;
public class JsonPlantDatabase implements PlantDatabase {
private final URL dataSource = getClass().getResource("plantdb.json");
private final static JavaTimeModule timeModule = new JavaTimeModule();
static {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
timeModule.addDeserializer(MonthDay.class, dateDeserializer);
}
@Override
public List<Plant> getPlantList(HardinessZone zone) throws IOException {
List<Plant> result = Collections.emptyList();
if (dataSource != null) {
ObjectMapper mapper = new ObjectMapper();
DateFormat dateFormat = new SimpleDateFormat("MM-dd");
mapper.setDateFormat(dateFormat);
mapper.registerModule(timeModule);
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
}