Skip to content
Snippets Groups Projects
Commit b5637d7d authored by Tor-Einar Skog's avatar Tor-Einar Skog
Browse files

Adding custom (de)serialization

parent c3760216
No related branches found
No related tags found
No related merge requests found
......@@ -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-->
......
/*
* 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;
}
}
/*
* 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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment