diff --git a/pom.xml b/pom.xml index d8d8289fbde5e4a1579db0b1c59bf4206c0f1e3d..4c7af6c1e08588d64e0ddeac563b8afa5f5b1eb2 100755 --- a/pom.xml +++ b/pom.xml @@ -111,6 +111,11 @@ <artifactId>jts</artifactId> <version>1.13</version> </dependency> +<dependency> + <groupId>com.bedatadriven</groupId> + <artifactId>jackson-datatype-jts</artifactId> + <version>2.2</version> +</dependency> <dependency> <groupId>com.github.bjornharrtell</groupId> <!--groupId>org.wololo</groupId--> diff --git a/src/main/java/no/nibio/vips/entity/ResultDeserializer.java b/src/main/java/no/nibio/vips/entity/ResultDeserializer.java new file mode 100644 index 0000000000000000000000000000000000000000..931409c7863822a88d05bd3b5e92ea15dd200615 --- /dev/null +++ b/src/main/java/no/nibio/vips/entity/ResultDeserializer.java @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2018 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.entity; + +import com.bedatadriven.jackson.datatype.jts.JtsModule; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; +import java.io.IOException; +import java.util.Date; +import java.util.Map; + +/** + * @copyright 2018 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +public class ResultDeserializer extends StdDeserializer<Result>{ + + public ResultDeserializer(){ + this(null); + } + + public ResultDeserializer(Class<?>vc){ + super(vc); + } + + @Override + public Result deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.registerModule(new JtsModule()); + Result r = new ResultImpl(); + JsonNode node = jp.getCodec().readTree(jp); + r.setValidTimeStart(objectMapper.convertValue(node.get("validTimeStart"), Date.class)); + r.setValidTimeEnd(objectMapper.convertValue(node.get("validTimeEnd"), Date.class)); + if(node.get("allValues") != null) + { + r.setAllValues(objectMapper.readValue(node.get("allValues").asText(), Map.class)); + } + r.setWarningStatus(node.get("warningStatus").asInt()); + JsonNode geomNode = node.get("validGeometry"); + Geometry validGeometry = null; + if(geomNode != null && geomNode.get("type") != null) + { + switch(geomNode.get("type").asText()){ + case "Point": + validGeometry = objectMapper.convertValue(geomNode, Point.class); + break; + case "Polygon": + validGeometry = objectMapper.convertValue(geomNode, Polygon.class); + break; + case "LineString": + validGeometry = objectMapper.convertValue(geomNode, LineString.class); + break; + default: + validGeometry = null; + } + } + r.setValidGeometry(validGeometry); + return r; + } + +} diff --git a/src/main/java/no/nibio/vips/entity/ResultSerializer.java b/src/main/java/no/nibio/vips/entity/ResultSerializer.java new file mode 100644 index 0000000000000000000000000000000000000000..ec08fdbcb19b83a71b6dbda7e6c1e74241b2c442 --- /dev/null +++ b/src/main/java/no/nibio/vips/entity/ResultSerializer.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2018 NIBIO <http://www.nibio.no/>. + * + * This file is part of VIPSCommon. + * VIPSCommon is free software: you can redistribute it and/or modify + * it under the terms of the NIBIO Open Source License as published by + * NIBIO, either version 1 of the License, or (at your option) any + * later version. + * + * VIPSCommon is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * NIBIO Open Source License for more details. + * + * You should have received a copy of the NIBIO Open Source License + * along with VIPSCommon. If not, see <http://www.nibio.no/licenses/>. + * + */ + +package no.nibio.vips.entity; + +import com.bedatadriven.jackson.datatype.jts.JtsModule; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.LineString; +import com.vividsolutions.jts.geom.Point; +import com.vividsolutions.jts.geom.Polygon; +import java.io.IOException; + +/** + * Ref http://www.baeldung.com/jackson-custom-serialization + * @copyright 2018 <a href="http://www.nibio.no/">NIBIO</a> + * @author Tor-Einar Skog <tor-einar.skog@nibio.no> + */ +class ResultSerializer extends StdSerializer<ResultImpl>{ + ObjectMapper objectMapper; + GeometryFactory gf; + public ResultSerializer() { + this(null); + } + + public ResultSerializer(Class<ResultImpl> t) { + super(t); + this.objectMapper = new ObjectMapper(); + this.objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); + this.objectMapper.registerModule(new JtsModule()); + } + + @Override + public void serialize(ResultImpl t, JsonGenerator jg, SerializerProvider sp) throws IOException { + + jg.writeStartObject(); + jg.writeStringField("validTimeStart", this.objectMapper.writeValueAsString(t.getValidTimeStart()).replaceAll("\"", "")); + jg.writeStringField("validTimeEnd", this.objectMapper.writeValueAsString(t.getValidTimeEnd()).replaceAll("\"", "")); + String geoJSON = t.getValidGeometry() == null ? "null" + : t.getValidGeometry().getGeometryType().equals("Point") ? objectMapper.writeValueAsString((Point) t.getValidGeometry()) + : t.getValidGeometry().getGeometryType().equals("LineString") ? objectMapper.writeValueAsString((LineString) t.getValidGeometry()) + :t.getValidGeometry().getGeometryType().equals("Polygon") ? objectMapper.writeValueAsString((Polygon) t.getValidGeometry()) + : "null"; + jg.writeStringField("validGeometry", geoJSON); + jg.writeNumberField("warningStatus", t.getWarningStatus()); + jg.writeStringField("allValues", this.objectMapper.writeValueAsString(t.getAllValues())); + jg.writeStringField("keys", this.objectMapper.writeValueAsString(t.getKeys())); + jg.writeEndObject(); + } + +}