Commit 3a1bb2f9 authored by Noel Alonso's avatar Noel Alonso
Browse files

Añade soporte para serializar y deserializar Point

Añade test
parent 0cbaa6d2
Loading
Loading
Loading
Loading
+4 −21
Original line number Diff line number Diff line
# Template
# JTS4Jackson

| Metrics | Master | Develop |
|:-:|:-:|:-:|
| CI status | [![pipeline status](https://gitlab.com/redmic-project/server/library/template/badges/master/pipeline.svg)](https://gitlab.com/redmic-project/server/library/template/commits/master) | [![pipeline status](https://gitlab.com/redmic-project/server/library/template/badges/dev/pipeline.svg)](https://gitlab.com/redmic-project/server/library/template/commits/dev) |
| Test coverage | [![coverage report](https://gitlab.com/redmic-project/server/library/template/badges/master/coverage.svg)](https://gitlab.com/redmic-project/server/library/template/commits/master) | [![coverage report](https://gitlab.com/redmic-project/server/library/template/badges/dev/coverage.svg)](https://gitlab.com/redmic-project/server/library/template/commits/dev) |
| CI status | [![pipeline status](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/badges/master/pipeline.svg)](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/commits/master) | [![pipeline status](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/badges/dev/pipeline.svg)](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/commits/dev) |
| Test coverage | [![coverage report](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/badges/master/coverage.svg)](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/commits/master) | [![coverage report](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/badges/dev/coverage.svg)](https://gitlab.com/redmic-project/server/library/jts4jackson-lib/commits/dev) |

Este proyecto sirve de guía para la creación de una nueva librería.

1. Antes de importar el proyecto, en el fichero `pom.xml`:
	* Reemplazar `template` por el nombre que se le quiere dar a la librería.
	* Definir convenientemente `name`, `description` y `artifactId`.
	* Definir la versión de `redmic` (en la sección `parent`) con la más reciente.

2. Importar como proyecto maven.

3. Remplazar `template` por el nombre adecuado en la primera línea de este mismo fichero.

4. Añadir las dependencias deseadas.

5. Añadir en `src/test/java/es/redmic/` y `src/main/java/es/redmic/` un nuevo package con el nombre de la librería, como base donde se añadirá el código.

6. Añadir al fichero `pom.xml` del proyecto padre (`es.redmic.lib.libs`), el nombre de la nueva librería.

7. Actualizar la tabla inicial de estado, eliminar estas instrucciones y documentar adecuadamente.
Este proyecto tiene como objetivo añadir soporte para serialización/deserialización de objetos JTS vía jackson
+46 −12
Original line number Diff line number Diff line
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

	<parent>
@@ -10,11 +11,11 @@

	<modelVersion>4.0.0</modelVersion>
	<groupId>es.redmic.lib</groupId>
	<artifactId>template-lib</artifactId>
	<artifactId>jts4jackson</artifactId>
	<packaging>jar</packaging>
	<version>x.y.z</version>
	<name>template-lib</name>
	<description>template-lib description</description>
	<version>0.0.1</version>
	<name>jts4jackson</name>
	<description>Jts serialization and deserialization for Jackson</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@@ -26,6 +27,8 @@
		<!-- REDMIC -->

		<!-- OTHERS -->
		<jts-core.version>1.16.0</jts-core.version>
		<spatial4j.version>0.7</spatial4j.version>

		<!-- Version plugins -->
		<jacoco.version>0.8.1</jacoco.version>
@@ -36,6 +39,29 @@

	<dependencies>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
		</dependency>
		
		<dependency>
			<groupId>org.locationtech.jts</groupId>
			<artifactId>jts-core</artifactId>
			<version>${jts-core.version}</version>
			<exclusions>
				<exclusion>
					<groupId>xerces</groupId>
					<artifactId>xercesImpl</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.locationtech.spatial4j</groupId>
			<artifactId>spatial4j</artifactId>
			<version>${spatial4j.version}</version>
		</dependency>

		<!-- Logs -->
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
@@ -47,6 +73,14 @@
			<artifactId>log4j-core</artifactId>
			<scope>provided</scope>
		</dependency>
		
		<!-- Tests -->
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
	<distributionManagement>
		<repository>
+40 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.module;

import org.locationtech.jts.geom.Point;

import com.fasterxml.jackson.core.json.PackageVersion;
import com.fasterxml.jackson.databind.module.SimpleModule;

import es.redmic.jts4jackson.point.PointDeserializer;
import es.redmic.jts4jackson.point.PointSerializer;

public class JTSModule extends SimpleModule {

	private static final long serialVersionUID = 1L;

	public JTSModule() {

		super(PackageVersion.VERSION);

		// deserializers
		addDeserializer(Point.class, new PointDeserializer());

		// serializers:
		addSerializer(Point.class, new PointSerializer());
	}

	@Override
	public String getModuleName() {
		return getClass().getSimpleName();
	}

	@Override
	public int hashCode() {
		return getClass().hashCode();
	}

	@Override
	public boolean equals(Object o) {
		return this == o;
	}
}
+30 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.point;

import java.io.IOException;

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.Point;
import org.locationtech.spatial4j.io.jackson.GeometryDeserializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class PointDeserializer extends JsonDeserializer<Point> {

	final GeometryDeserializer dser;

	public PointDeserializer() {
		dser = new GeometryDeserializer();
	}

	@Override
	public Point deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		Geometry geometry = dser.deserialize(jp, ctxt);
		if (geometry != null) {
			return (Point) geometry;
		}
		return null;
	}
}
+24 −0
Original line number Diff line number Diff line
package es.redmic.jts4jackson.point;

import java.io.IOException;

import org.locationtech.jts.geom.Point;
import org.locationtech.spatial4j.io.jackson.GeometryAsGeoJSONSerializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class PointSerializer extends JsonSerializer<Point> {

	final GeometryAsGeoJSONSerializer ser;

	public PointSerializer() {
		ser = new GeometryAsGeoJSONSerializer();
	}

	@Override
	public void serialize(Point value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		ser.serialize(value, gen, serializers);
	}
}
Loading