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

Assorted adaptations to JSONB which we currently avoid using

parent 665cf65d
No related branches found
No related tags found
No related merge requests found
......@@ -29,9 +29,10 @@ import com.vividsolutions.jts.geom.Geometry;
/**
* Represents a result from a model run
* @copyright 2013 {@link http://www.nibio.no NIBIO}
* @author Tor-Einar Skog <tor-einar.skog@hyper.no>
* @copyright 2013-2019 {@link http://www.nibio.no NIBIO}
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
//@JsonbTypeAdapter(value = ResultJsonbAdapter.class)
@JsonDeserialize(using=ResultDeserializer.class)
@JsonSerialize(using=ResultSerializer.class)
public interface Result extends Comparable{
......
/*
* Copyright (c) 2019 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.databind.ObjectMapper;
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.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.json.JsonValue;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.adapter.JsonbAdapter;
import no.nibio.vips.gis.GISUtil;
/**
* @copyright 2019 <a href="http://www.nibio.no/">NIBIO</a>
* @author Tor-Einar Skog <tor-einar.skog@nibio.no>
*/
public class ResultJsonbAdapter implements JsonbAdapter<Result, JsonObject>{
@Override
public JsonObject adaptToJson(Result original) throws Exception {
System.out.println("adaptToJson!");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JtsModule());
JsonObjectBuilder builder = Json.createObjectBuilder();
builder.add("validTimeStart", objectMapper.writeValueAsString(original.getValidTimeStart()).replaceAll("\"", ""));
builder.add("validTimeEnd", objectMapper.writeValueAsString(original.getValidTimeStart()).replaceAll("\"", ""));
System.out.println("adaptToJson1");
String geoJSON = original.getValidGeometry() == null ? "null"
: original.getValidGeometry().getGeometryType().equals("Point") ? objectMapper.writeValueAsString((Point) original.getValidGeometry())
: original.getValidGeometry().getGeometryType().equals("LineString") ? objectMapper.writeValueAsString((LineString) original.getValidGeometry())
: original.getValidGeometry().getGeometryType().equals("Polygon") ? objectMapper.writeValueAsString((Polygon) original.getValidGeometry())
: "null";
builder.add("validGeometry", geoJSON);
System.out.println("adaptToJson2");
builder.add("warningStatus", original.getWarningStatus());
System.out.println("adaptToJson3");
builder.add("allValues", objectMapper.writeValueAsString(original.getAllValues()));
System.out.println("adaptToJson4");
builder.add("keys", objectMapper.writeValueAsString(original.getKeys()));
System.out.println("adaptToJson5");
return builder.build();
}
@Override
public Result adaptFromJson(JsonObject adapted) throws Exception {
System.out.println("adaptFromJson!");
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JtsModule());
Result r = new ResultImpl();
r.setValidTimeStart(objectMapper.convertValue(adapted.getString("validTimeStart"), Date.class));
r.setValidTimeEnd(objectMapper.convertValue(adapted.getString("validTimeEnd"), Date.class));
System.out.println("adaptFromJson1");
if(adapted.get("allValues") != null)
{
r.setAllValues(objectMapper.readValue(adapted.getString("allValues"), Map.class));
System.out.println("adaptFromJson2");
}
r.setWarningStatus(adapted.getInt("warningStatus"));
//System.out.println(adapted.getString("validGeometry"));
Geometry validGeometry = null;
//System.out.println(geomNode);
if(!adapted.isNull("validGeometry"))
{
JsonObject geomNode = adapted.getValue("validGeometry").asJsonObject();
switch(geomNode.getString("type")){
case "Point":
validGeometry = objectMapper.convertValue(adapted.getString("validGeometry"), Point.class);
break;
case "Polygon":
validGeometry = objectMapper.convertValue(adapted.getString("validGeometry"), Polygon.class);
break;
case "LineString":
validGeometry = objectMapper.convertValue(adapted.getString("validGeometry"), LineString.class);
break;
default:
validGeometry = null;
}
if(validGeometry.getSRID() <= 0)
{
validGeometry.setSRID(GISUtil.DEFAULT_SRID);
}
}
r.setValidGeometry(validGeometry);
//System.out.println(r.getValidGeometry());
return r;
}
}
......@@ -20,6 +20,7 @@
package no.nibio.vips.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.Date;
import javax.json.bind.annotation.JsonbDateFormat;
import javax.json.bind.annotation.JsonbTransient;
......@@ -143,8 +144,9 @@ public class WeatherObservation implements Comparable{
this.value = value;
}
//@JsonIgnore
@JsonbTransient
//@JsonbTransient
@JsonIgnore
public long getValiditySignature()
{
long result = 17;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment