Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision

Target

Select target project
  • bragearn/examples
  • johngu/examples
  • krisschn/examples
  • erikbd/examples
  • helenesm/examples
  • balazor/examples
  • htechter/examples
  • aslakho/examples
  • jonri/examples
  • andreski/examples
  • sigriksc/examples
  • norasbr/examples
  • jannash/examples
  • smledsaa/examples
  • mtorre/examples
  • tdt4140-staff/examples
16 results
Select Git revision
Show changes
Showing
with 750 additions and 79 deletions
package tdt4140.gr1800.app.doc;
/**
* Listener interface for the (current) location of the (current) document of an IDocumentStorage, e.g.
* when a **save-as** action is performed.
* @author hal
*
* @param <L>
*/
public interface IDocumentStorageListener<L> {
/**
* Notifies that the current document location has changed.
* @param documentLocation the new document location
* @param oldDocumentLocation the previous document location
*/
public void documentLocationChanged(L documentLocation, L oldDocumentLocation);
}
package tdt4140.gr1800.app.geojson;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import org.geojson.GeoJsonObject;
import org.geojson.LngLatAlt;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GeoJsonDocumentConverter implements IDocumentLoader<Collection<GeoLocations>> {
private GeoJsonDocumentLoader geoJsonLoader = new GeoJsonDocumentLoader();
@Override
public Collection<GeoLocations> loadDocument(InputStream inputStream) throws Exception {
GeoJsonObject geoJson = geoJsonLoader.loadDocument(inputStream);
return convert(geoJson);
}
private String trackNameFormat = "%s";
private String trackCountFormat = "Track %s";
private String trackSegmentFormat = "%s.%s";
public void setTrackNameFormat(String trackNameFormat) {
this.trackNameFormat = trackNameFormat;
}
public void setTrackCountFormat(String trackCountFormat) {
this.trackCountFormat = trackCountFormat;
}
public void setTrackSegmentFormat(String trackSegmentFormat) {
this.trackSegmentFormat = trackSegmentFormat;
}
private String routeNameFormat = "%s";
private String routeCountFormat = "Route %s";
public void setRouteNameFormat(String routeNameFormat) {
this.routeNameFormat = routeNameFormat;
}
public void setRouteCountFormat(String routeCountFormat) {
this.routeCountFormat = routeCountFormat;
}
public Collection<GeoLocations> convert(GeoJsonObject geoJson) throws Exception {
Collection<GeoLocations> geoLocations = new ArrayList<GeoLocations>();
return geoLocations;
}
private LatLong[] convert(Collection<LngLatAlt> points) {
Collection<LatLong> latLongs = points.stream()
.map(point -> convert(point))
.collect(Collectors.toList());
return latLongs.toArray(new LatLong[latLongs.size()]);
}
private LatLong convert(LngLatAlt point) {
return new LatLong(point.getLatitude(), point.getLongitude());
}
}
package tdt4140.gr1800.app.geojson;
import java.io.InputStream;
import org.geojson.GeoJsonObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GeoJsonDocumentLoader implements IDocumentLoader<GeoJsonObject> {
@Override
public GeoJsonObject loadDocument(InputStream inputStream) throws Exception {
return new ObjectMapper().readValue(inputStream, GeoJsonObject.class);
}
}
package tdt4140.gr1800.app.gpx;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.stream.Collectors;
import io.jenetics.jpx.GPX;
import io.jenetics.jpx.Route;
import io.jenetics.jpx.Track;
import io.jenetics.jpx.TrackSegment;
import io.jenetics.jpx.WayPoint;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GpxDocumentConverter implements IDocumentLoader<Collection<GeoLocations>> {
private GpxDocumentLoader gpxLoader = new GpxDocumentLoader();
@Override
public Collection<GeoLocations> loadDocument(InputStream inputStream) throws Exception {
GPX gpx = gpxLoader.loadDocument(inputStream);
return convert(gpx);
}
private String trackNameFormat = "%s";
private String trackCountFormat = "Track %s";
private String trackSegmentFormat = "%s.%s";
public void setTrackNameFormat(String trackNameFormat) {
this.trackNameFormat = trackNameFormat;
}
public void setTrackCountFormat(String trackCountFormat) {
this.trackCountFormat = trackCountFormat;
}
public void setTrackSegmentFormat(String trackSegmentFormat) {
this.trackSegmentFormat = trackSegmentFormat;
}
private String routeNameFormat = "%s";
private String routeCountFormat = "Route %s";
public void setRouteNameFormat(String routeNameFormat) {
this.routeNameFormat = routeNameFormat;
}
public void setRouteCountFormat(String routeCountFormat) {
this.routeCountFormat = routeCountFormat;
}
public Collection<GeoLocations> convert(GPX gpx) throws Exception {
Collection<GeoLocations> geoLocations = new ArrayList<GeoLocations>();
int trackCount = 1;
for (Track track : gpx.getTracks()) {
String trackName = (track.getName().isPresent() ? String.format(trackNameFormat, track.getName().get()) : String.format(trackCountFormat, trackCount));
int segmentCount = 1;
for (TrackSegment segment : track) {
boolean singleTrack = track.getSegments().size() <= 1;
String name = (singleTrack ? trackName : String.format(trackSegmentFormat, trackName, segmentCount));
GeoLocations gl = new GeoLocations(name, convert(segment.getPoints()));
gl.setPath(true);
gl.addTags(Track.class.getName().toLowerCase());
geoLocations.add(gl);
}
}
int routeCount = 1;
for (Route route : gpx.getRoutes()) {
String routeName = (route.getName().isPresent() ? String.format(routeNameFormat, route.getName().get()) : String.format(routeCountFormat, routeCount));
GeoLocations gl = new GeoLocations(routeName, convert(route.getPoints()));
gl.setPath(true);
gl.addTags(Route.class.getName().toLowerCase());
geoLocations.add(gl);
}
if (! gpx.getWayPoints().isEmpty()) {
GeoLocations gl = new GeoLocations("Waypoints", convert(gpx.getWayPoints()));
gl.addTags(WayPoint.class.getName().toLowerCase());
geoLocations.add(gl);
}
return geoLocations;
}
private LatLong[] convert(Collection<WayPoint> points) {
Collection<LatLong> latLongs = points.stream()
.map(point -> convert(point))
.collect(Collectors.toList());
return latLongs.toArray(new LatLong[latLongs.size()]);
}
private LatLong convert(WayPoint point) {
return new LatLong(point.getLatitude().doubleValue(), point.getLongitude().doubleValue());
}
}
package tdt4140.gr1800.app.gpx;
import java.io.InputStream;
import io.jenetics.jpx.GPX;
import tdt4140.gr1800.app.doc.IDocumentLoader;
public class GpxDocumentLoader implements IDocumentLoader<GPX> {
@Override
public GPX loadDocument(InputStream inputStream) throws Exception {
return GPX.read(inputStream, true);
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import tdt4140.gr1800.app.core.GeoLocated;
public class GeoLocatedSerializer<T extends GeoLocated> extends StdSerializer<T> {
public static final String LONGITUDE_FIELD_NAME = "longitude";
public static final String LATITUDE_FIELD_NAME = "latitude";
public GeoLocatedSerializer(Class<T> clazz) {
super(clazz);
}
protected void serialize(GeoLocated geoLocated, JsonGenerator jsonGen) throws IOException {
jsonGen.writeFieldName(LATITUDE_FIELD_NAME);
jsonGen.writeNumber(geoLocated.getLatitude());
jsonGen.writeFieldName(LONGITUDE_FIELD_NAME);
jsonGen.writeNumber(geoLocated.getLongitude());
}
@Override
public void serialize(T value, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {
serialize(value, jsonGen);
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.GeoLocation;
import tdt4140.gr1800.app.core.GeoLocations;
public class GeoLocationDeserializer extends StdDeserializer<GeoLocation> {
public GeoLocationDeserializer() {
super(GeoLocations.class);
}
@Override
public GeoLocation deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode);
}
private final TimedTaggedImplDeserializer timedTaggedImplDeserializer = new TimedTaggedImplDeserializer();
private final LatLongDeserializer latLongDeserializer = new LatLongDeserializer();
GeoLocation deserialize(final JsonNode jsonNode) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final GeoLocation geoLocation = new GeoLocation();
if (objectNode.has(GeoLocationsSerializer.NAME_FIELD_NAME)) {
final String name = objectNode.get(GeoLocationsSerializer.NAME_FIELD_NAME).asText();
geoLocation.setName(name);
}
if (objectNode.has(GeoLocationsSerializer.DESCRIPTION_FIELD_NAME)) {
final String description = objectNode.get(GeoLocationsSerializer.DESCRIPTION_FIELD_NAME).asText();
geoLocation.setDescription(description);
}
if (objectNode.has("location")) {
final JsonNode locationNode = objectNode.get("location");
geoLocation.setLatLong(latLongDeserializer.deserialize(locationNode));
} else {
geoLocation.setLatLong(latLongDeserializer.deserialize(objectNode));
}
if (objectNode.has(GeoLocationSerializer.ELEVATION_FIELD_NAME)) {
final int elevation = objectNode.get(GeoLocationSerializer.ELEVATION_FIELD_NAME).asInt();
geoLocation.setElevation(elevation);
}
timedTaggedImplDeserializer.deserialize(jsonNode, geoLocation);
return geoLocation;
} else if (jsonNode instanceof ArrayNode) {
final GeoLocation geoLocation = new GeoLocation();
geoLocation.setLatLong(latLongDeserializer.deserialize(jsonNode));
return geoLocation;
}
return null;
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import tdt4140.gr1800.app.core.GeoLocation;
public class GeoLocationSerializer extends TimedTaggedSerializer<GeoLocation> {
public static final String ELEVATION_FIELD_NAME = "elevation";
public GeoLocationSerializer() {
super(GeoLocation.class);
}
private GeoLocatedSerializer<GeoLocation> geoLocatedSerializer = new GeoLocatedSerializer<>(GeoLocation.class);
@Override
public void serialize(GeoLocation geoLocation, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException {
jsonGen.writeStartObject();
super.serialize(geoLocation, jsonGen);
geoLocatedSerializer.serialize(geoLocation, jsonGen);
if (geoLocation.getElevation() != 0) {
jsonGen.writeFieldName(ELEVATION_FIELD_NAME);
jsonGen.writeNumber(geoLocation.getElevation());
}
if (geoLocation.getName() != null) {
jsonGen.writeFieldName(GeoLocationsSerializer.NAME_FIELD_NAME);
jsonGen.writeString(geoLocation.getName());
}
if (geoLocation.getDescription() != null) {
jsonGen.writeFieldName(GeoLocationsSerializer.DESCRIPTION_FIELD_NAME);
jsonGen.writeString(geoLocation.getDescription());
}
jsonGen.writeEndObject();
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.GeoLocation;
import tdt4140.gr1800.app.core.GeoLocations;
public class GeoLocationsDeserializer extends StdDeserializer<GeoLocations> {
public GeoLocationsDeserializer() {
super(GeoLocations.class);
}
@Override
public GeoLocations deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode, deserContext);
}
private final GeoLocationDeserializer geoLocationDeserializer = new GeoLocationDeserializer();
GeoLocations deserialize(final JsonNode jsonNode, final DeserializationContext deserContext) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final String name = objectNode.get(GeoLocationsSerializer.NAME_FIELD_NAME).asText();
final GeoLocations geoLocations = new GeoLocations(name);
final JsonNode pathNode = objectNode.get(GeoLocationsSerializer.PATH_FIELD_NAME);
geoLocations.setPath(pathNode != null && pathNode.asBoolean(false));
final JsonNode locationsNode = objectNode.get(GeoLocationsSerializer.LOCATIONS_FIELD_NAME);
if (locationsNode instanceof ArrayNode) {
for (final JsonNode locationNode : (ArrayNode) locationsNode) {
final GeoLocation geoLocation = geoLocationDeserializer.deserialize(locationNode);
geoLocations.addLocation(geoLocation);
}
}
return geoLocations;
}
return null;
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
public class GeoLocationsJsonDeserializer extends StdDeserializer<GeoLocations> {
public GeoLocationsJsonDeserializer() {
super(GeoLocations.class);
}
@Override
public GeoLocations deserialize(JsonParser jsonParser, DeserializationContext deserContext) throws IOException, JsonProcessingException {
JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode);
}
private GeoLocations deserialize(JsonNode jsonNode) throws IOException, JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) jsonNode;
String name = objectNode.get(GeoLocationsJsonSerializer.NAME_FIELD_NAME).asText();
GeoLocations geoLocations = new GeoLocations(name);
JsonNode pathNode = objectNode.get(GeoLocationsJsonSerializer.PATH_FIELD_NAME);
geoLocations.setPath(pathNode != null && pathNode.asBoolean(false));
JsonNode locationsNode = objectNode.get(GeoLocationsJsonSerializer.LOCATIONS_FIELD_NAME);
if (locationsNode instanceof ArrayNode) {
for (JsonNode locationNode : (ArrayNode) locationsNode) {
LatLong latLong = decodeLatLong(locationNode);
geoLocations.addLocation(latLong);
}
} else {
LatLong latLong = decodeLatLong(locationsNode);
geoLocations.addLocation(latLong);
}
return geoLocations;
}
return null;
}
private LatLong decodeLatLong(JsonNode locationNode) {
LatLong latLong = null;
if (locationNode instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode) locationNode;
if (objectNode.has(GeoLocationsJsonSerializer.LATITUDE_FIELD_NAME) && objectNode.has(GeoLocationsJsonSerializer.LONGITUDE_FIELD_NAME)) {
double lat = objectNode.get(GeoLocationsJsonSerializer.LATITUDE_FIELD_NAME).asDouble();
double lon = objectNode.get(GeoLocationsJsonSerializer.LONGITUDE_FIELD_NAME).asDouble();
latLong = new LatLong(lat, lon);
}
} else if (locationNode instanceof ArrayNode) {
ArrayNode arrayNode = (ArrayNode) locationNode;
if (arrayNode.size() == 2) {
double lat = arrayNode.get(0).asDouble();
double lon = arrayNode.get(1).asDouble();
latLong = new LatLong(lat, lon);
}
}
return latLong;
}
}
......@@ -8,28 +8,37 @@ import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import tdt4140.gr1800.app.core.GeoLocation;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.GeoLocationsPersistence;
import tdt4140.gr1800.app.core.GeoLocationsStreamPersistence;
import tdt4140.gr1800.app.core.LatLong;
import tdt4140.gr1800.app.core.Person;
public class GeoLocationsJsonPersistence implements GeoLocationsPersistence {
public class GeoLocationsPersistence implements GeoLocationsStreamPersistence {
private final ObjectMapper objectMapper;
public GeoLocationsJsonPersistence() {
public GeoLocationsPersistence() {
objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(new GeoLocationsJsonSerializer());
module.addDeserializer(GeoLocations.class, new GeoLocationsJsonDeserializer());
final SimpleModule module = new SimpleModule();
module.addSerializer(new LatLongSerializer());
module.addSerializer(new GeoLocationSerializer());
module.addSerializer(new GeoLocationsSerializer());
module.addSerializer(new PersonSerializer());
module.addDeserializer(LatLong.class, new LatLongDeserializer());
module.addDeserializer(GeoLocation.class, new GeoLocationDeserializer());
module.addDeserializer(GeoLocations.class, new GeoLocationsDeserializer());
module.addDeserializer(Person.class, new PersonDeserializer());
objectMapper.registerModule(module);
}
@Override
public Collection<GeoLocations> loadLocations(InputStream inputStream) throws Exception {
public Collection<GeoLocations> loadLocations(final InputStream inputStream) throws Exception {
return objectMapper.readValue(inputStream, objectMapper.getTypeFactory().constructCollectionType(List.class, GeoLocations.class));
}
@Override
public void saveLocations(Collection<GeoLocations> geoLocations, OutputStream outputStream) throws Exception {
public void saveLocations(final Collection<GeoLocations> geoLocations, final OutputStream outputStream) throws Exception {
objectMapper.writeValue(outputStream, geoLocations);
}
}
......@@ -6,37 +6,40 @@ import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import tdt4140.gr1800.app.core.GeoLocated;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.LatLong;
public class GeoLocationsJsonSerializer extends StdSerializer<GeoLocations> {
public class GeoLocationsSerializer extends StdSerializer<GeoLocations> {
public static final String LONGITUDE_FIELD_NAME = "longitude";
public static final String LATITUDE_FIELD_NAME = "latitude";
public static final String LOCATIONS_FIELD_NAME = "locations";
public static final String PATH_FIELD_NAME = "path";
public static final String NAME_FIELD_NAME = "name";
public static final String DESCRIPTION_FIELD_NAME = "description";
public GeoLocationsJsonSerializer() {
public GeoLocationsSerializer() {
super(GeoLocations.class);
}
private TimedTaggedSerializer<GeoLocations> timedTaggedSerializer = new TimedTaggedSerializer<>(GeoLocations.class);
@Override
public void serialize(GeoLocations geoLocations, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException {
jsonGen.writeStartObject();
jsonGen.writeFieldName(NAME_FIELD_NAME);
jsonGen.writeString(geoLocations.getName());
if (geoLocations.getDescription() != null) {
jsonGen.writeFieldName(DESCRIPTION_FIELD_NAME);
jsonGen.writeString(geoLocations.getDescription());
}
jsonGen.writeFieldName(PATH_FIELD_NAME);
jsonGen.writeBoolean(geoLocations.isPath());
jsonGen.writeFieldName(LOCATIONS_FIELD_NAME);
jsonGen.writeStartArray();
for (LatLong latLon : geoLocations) {
jsonGen.writeStartArray();
jsonGen.writeNumber(latLon.latitude);
jsonGen.writeNumber(latLon.longitude);
jsonGen.writeEndArray();
for (GeoLocated geoLoc : geoLocations) {
jsonGen.writeObject(geoLoc);
}
jsonGen.writeEndArray();
timedTaggedSerializer.serialize(geoLocations, jsonGen);
jsonGen.writeEndObject();
}
}
package tdt4140.gr1800.app.json;
import java.util.function.Consumer;
import java.util.function.Function;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
public abstract class IdDeserializer<T> extends StdDeserializer<T> {
public IdDeserializer(final Class<T> clazz) {
super(clazz);
}
void deserialize(final ObjectNode objectNode, final String fieldName, final Consumer<String> setter) throws JsonProcessingException {
if (objectNode.has(fieldName)) {
final String s = objectNode.get(fieldName).asText();
setter.accept(s);
}
}
<U> void deserialize(final ObjectNode objectNode, final String fieldName, final Function<String, U> parser, final Consumer<U> setter) throws JsonProcessingException {
if (objectNode.has(fieldName)) {
final String s = objectNode.get(fieldName).asText();
final U value = parser.apply(s);
setter.accept(value);
}
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import tdt4140.gr1800.app.db.IdProvider;
public abstract class IdSerializer<T> extends StdSerializer<T> {
public static final String ID_FIELD_NAME = "id";
public IdSerializer(final Class<T> clazz) {
super(clazz);
}
private IdProvider<T> idProvider = null;
public void setIdProvider(final IdProvider<T> idProvider) {
this.idProvider = idProvider;
}
protected void serializeId(final T t, final JsonGenerator jsonGen) throws IOException {
if (idProvider != null && idProvider.hasId(t)) {
jsonGen.writeFieldName(ID_FIELD_NAME);
jsonGen.writeNumber(idProvider.getId(t));
}
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.LatLong;
public class LatLongDeserializer extends StdDeserializer<LatLong> {
public LatLongDeserializer() {
super(LatLong.class);
}
@Override
public LatLong deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode);
}
public LatLong deserialize(final JsonNode jsonNode) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final double latitude = objectNode.get(GeoLocatedSerializer.LATITUDE_FIELD_NAME).asDouble();
final double longitude = objectNode.get(GeoLocatedSerializer.LONGITUDE_FIELD_NAME).asDouble();
return new LatLong(latitude, longitude);
} else if (jsonNode instanceof ArrayNode) {
final ArrayNode locationArray = (ArrayNode) jsonNode;
if (locationArray.size() == 2) {
final double latitude = locationArray.get(0).asDouble();
final double longitude = locationArray.get(1).asDouble();
return new LatLong(latitude, longitude);
}
}
return null;
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import tdt4140.gr1800.app.core.LatLong;
public class LatLongSerializer extends GeoLocatedSerializer<LatLong> {
public LatLongSerializer() {
super(LatLong.class);
}
@Override
public void serialize(LatLong geoLocated, JsonGenerator jsonGen, SerializerProvider serProvider) throws IOException {
jsonGen.writeStartObject();
super.serialize(geoLocated, jsonGen);
jsonGen.writeEndObject();
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
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.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.Person;
public class PersonDeserializer extends IdDeserializer<Person> {
public PersonDeserializer() {
super(Person.class);
}
@Override
public Person deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode, deserContext);
}
private final GeoLocationsDeserializer geoLocationsDeserializer = new GeoLocationsDeserializer();
Person deserialize(final JsonNode jsonNode, final DeserializationContext deserContext) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
final Person person = new Person();
deserialize(objectNode, PersonSerializer.NAME_FIELD_NAME, person::setName);
deserialize(objectNode, PersonSerializer.EMAIL_FIELD_NAME, person::setEmail);
if (objectNode.has(GeoLocationsSerializer.LOCATIONS_FIELD_NAME)) {
final JsonNode locationsNode = objectNode.get(GeoLocationsSerializer.LOCATIONS_FIELD_NAME);
if (locationsNode instanceof ArrayNode) {
for (final JsonNode childNode : ((ArrayNode) locationsNode)) {
final GeoLocations geoLocations = geoLocationsDeserializer.deserialize(childNode, deserContext);
person.addGeolocations(geoLocations);
}
}
}
return person;
}
return null;
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import tdt4140.gr1800.app.core.GeoLocations;
import tdt4140.gr1800.app.core.Person;
public class PersonSerializer extends IdSerializer<Person> {
public static final String ID_FIELD_NAME = "id";
public static final String NAME_FIELD_NAME = "name";
public static final String EMAIL_FIELD_NAME = "email";
public static final String GEOLOCATIONS_FIELD_NAME = "geoLocations";
public PersonSerializer() {
super(Person.class);
}
@Override
public void serialize(final Person person, final JsonGenerator jsonGen, final SerializerProvider serProvider) throws IOException {
jsonGen.writeStartObject();
serializeId(person, jsonGen);
jsonGen.writeFieldName(NAME_FIELD_NAME);
jsonGen.writeString(person.getName());
if (person.getEmail() != null) {
jsonGen.writeFieldName(EMAIL_FIELD_NAME);
jsonGen.writeString(person.getEmail());
}
jsonGen.writeFieldName(GEOLOCATIONS_FIELD_NAME);
jsonGen.writeStartArray();
for (final GeoLocations geoLocations : person.getGeoLocations((String[]) null)) {
jsonGen.writeObject(geoLocations);
}
jsonGen.writeEndArray();
jsonGen.writeEndObject();
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.ZoneId;
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.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.node.ObjectNode;
import tdt4140.gr1800.app.core.TimedTaggedImpl;
public class TimedTaggedImplDeserializer extends StdDeserializer<TimedTaggedImpl> {
public TimedTaggedImplDeserializer() {
super(TimedTaggedImpl.class);
}
@Override
public TimedTaggedImpl deserialize(final JsonParser jsonParser, final DeserializationContext deserContext) throws IOException, JsonProcessingException {
final JsonNode jsonNode = jsonParser.getCodec().readTree(jsonParser);
return deserialize(jsonNode, new TimedTaggedImpl());
}
public TimedTaggedImpl deserialize(final JsonNode jsonNode, final TimedTaggedImpl timedTagged) throws JsonProcessingException {
if (jsonNode instanceof ObjectNode) {
final ObjectNode objectNode = (ObjectNode) jsonNode;
if (objectNode.has(TimedTaggedSerializer.DATE_FIELD_NAME)) {
timedTagged.setDate(LocalDate.parse(objectNode.get(TimedTaggedSerializer.DATE_FIELD_NAME).asText()));
}
if (objectNode.has(TimedTaggedSerializer.TIME_FIELD_NAME)) {
timedTagged.setTime(LocalTime.parse(objectNode.get(TimedTaggedSerializer.TIME_FIELD_NAME).asText()));
}
if (objectNode.has(TimedTaggedSerializer.ZONE_FIELD_NAME)) {
timedTagged.setZone(ZoneId.of(objectNode.get(TimedTaggedSerializer.ZONE_FIELD_NAME).asText()));
}
}
return timedTagged;
}
}
package tdt4140.gr1800.app.json;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import tdt4140.gr1800.app.core.TimedTaggedImpl;
public class TimedTaggedSerializer<T extends TimedTaggedImpl> extends StdSerializer<T> {
public static final String DATE_FIELD_NAME = "date";
public static final String TIME_FIELD_NAME = "time";
public static final String ZONE_FIELD_NAME = "zone";
public static final String TAGS_FIELD_NAME = "tags";
public TimedTaggedSerializer(Class<T> clazz) {
super(clazz);
}
protected void serialize(TimedTaggedImpl timedTagged, JsonGenerator jsonGen) throws IOException {
if (timedTagged.getDate() != null) {
jsonGen.writeFieldName(DATE_FIELD_NAME);
jsonGen.writeString(timedTagged.getDate().toString());
}
if (timedTagged.getTime() != null) {
jsonGen.writeFieldName(TIME_FIELD_NAME);
jsonGen.writeString(timedTagged.getTime().toString());
}
String[] tags = timedTagged.getTags();
if (tags != null && tags.length > 0) {
jsonGen.writeFieldName(TAGS_FIELD_NAME);
jsonGen.writeStartArray();
for (int i = 0; i < tags.length; i++) {
jsonGen.writeString(tags[i]);
}
jsonGen.writeEndArray();
}
}
@Override
public void serialize(T value, JsonGenerator jsonGen, SerializerProvider provider) throws IOException {
serialize(value, jsonGen);
}
}