#36 Added images to Plant database
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package ch.zhaw.gartenverwaltung.io;
|
||||
|
||||
import ch.zhaw.gartenverwaltung.json.PlantImageDeserializer;
|
||||
import ch.zhaw.gartenverwaltung.types.HardinessZone;
|
||||
import ch.zhaw.gartenverwaltung.types.Plant;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.MonthDayDeserializer;
|
||||
import javafx.scene.image.Image;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
@@ -31,10 +34,14 @@ public class JsonPlantDatabase implements PlantDatabase {
|
||||
* Creating constant objects required to deserialize the {@link MonthDay} classes
|
||||
*/
|
||||
private final static JavaTimeModule timeModule = new JavaTimeModule();
|
||||
private final static SimpleModule imageModule = new SimpleModule();
|
||||
|
||||
static {
|
||||
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM-dd");
|
||||
MonthDayDeserializer dateDeserializer = new MonthDayDeserializer(dateFormat);
|
||||
timeModule.addDeserializer(MonthDay.class, dateDeserializer);
|
||||
|
||||
imageModule.addDeserializer(Image.class, new PlantImageDeserializer());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,8 +82,9 @@ public class JsonPlantDatabase implements PlantDatabase {
|
||||
}
|
||||
if (dataSource != null) {
|
||||
currentZone = zone;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(timeModule);
|
||||
ObjectMapper mapper = new ObjectMapper()
|
||||
.registerModule(timeModule)
|
||||
.registerModule(imageModule);
|
||||
|
||||
List<Plant> result;
|
||||
result = mapper.readerForListOf(Plant.class).readValue(dataSource);
|
||||
|
||||
Reference in New Issue
Block a user