Skip to content

Serializers in GeoJsonModule constructor. #4955

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,31 @@
import com.fasterxml.jackson.databind.node.ArrayNode;

/**
* A Jackson {@link Module} to register custom {@link JsonDeserializer}s for GeoJSON types.
* A Jackson {@link Module} to register custom {@link JsonDeserializer}s and {@link JsonSerializer}s for GeoJSON types.
* <br />
* Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and
* {@link JsonDeserializer deserializers}.
*
* @author Christoph Strobl
* @author Oliver Gierke
* @author Mark Paluch
* @author Artyom Muravlev
* @since 1.7
*/
public class GeoJsonModule extends SimpleModule {

private static final long serialVersionUID = -8723016728655643720L;
private static final Version VERSION = new Version(3,
2,
0,
null,
"org.springframework.data",
"spring-data-mongodb-geojson");

public GeoJsonModule() {

super("Spring Data MongoDB GeoJson", Version.unknownVersion());
registerDeserializersIn(this);
// TODO: add serializers as of next major version (4.0).
GeoJsonSerializersModule.registerSerializersIn(this);
}

/**
Expand All @@ -71,7 +78,7 @@ public GeoJsonModule() {
public static Module deserializers() {

SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Deserializers",
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
VERSION);
registerDeserializersIn(module);
return module;
}
Expand All @@ -93,7 +100,7 @@ public static Module deserializers() {
public static Module serializers() {

SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Serializers",
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
VERSION);
GeoJsonSerializersModule.registerSerializersIn(module);
return module;
}
Expand All @@ -115,12 +122,7 @@ public static Module serializers() {
* @since 3.2
*/
public static Module geoJsonModule() {

SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson",
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
GeoJsonSerializersModule.registerSerializersIn(module);
registerDeserializersIn(module);
return module;
return new GeoJsonModule();
}

private static void registerDeserializersIn(SimpleModule module) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,34 @@

import org.springframework.data.geo.Point;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
* @author Christoph Strobl
* @author Artyom Muravlev
*/
public class GeoJsonModuleUnitTests {
class GeoJsonModuleUnitTests {

ObjectMapper mapper;

@BeforeEach
public void setUp() {
void setUp() {

mapper = new ObjectMapper();
mapper.registerModule(new GeoJsonModule());
}

@Test // DATAMONGO-1181
public void shouldDeserializeJsonPointCorrectly() throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeJsonPointCorrectly() throws IOException {

String json = "{ \"type\": \"Point\", \"coordinates\": [10.0, 20.0] }";

assertThat(mapper.readValue(json, GeoJsonPoint.class)).isEqualTo(new GeoJsonPoint(10D, 20D));
}

@Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonLineStringCorrectly()
throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeGeoJsonLineStringCorrectly()
throws IOException {

String json = "{ \"type\": \"LineString\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}";

Expand All @@ -62,8 +61,8 @@ public void shouldDeserializeGeoJsonLineStringCorrectly()
}

@Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonMultiPointCorrectly()
throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeGeoJsonMultiPointCorrectly()
throws IOException {

String json = "{ \"type\": \"MultiPoint\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}";

Expand All @@ -73,8 +72,8 @@ public void shouldDeserializeGeoJsonMultiPointCorrectly()

@Test // DATAMONGO-1181
@SuppressWarnings("unchecked")
public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeGeoJsonMultiLineStringCorrectly()
throws IOException {

String json = "{ \"type\": \"MultiLineString\", \"coordinates\": [ [ [10.0, 20.0], [30.0, 40.0] ], [ [50.0, 60.0] , [70.0, 80.0] ] ]}";

Expand All @@ -83,7 +82,7 @@ public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
}

@Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeGeoJsonPolygonCorrectly() throws IOException {

String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";

Expand All @@ -92,8 +91,8 @@ public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException
}

@Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonMultiPolygonCorrectly()
throws JsonParseException, JsonMappingException, IOException {
void shouldDeserializeGeoJsonMultiPolygonCorrectly()
throws IOException {

String json = "{ \"type\": \"Polygon\", \"coordinates\": ["
+ "[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],"
Expand All @@ -110,4 +109,74 @@ public void shouldDeserializeGeoJsonMultiPolygonCorrectly()
new Point(100.2, 0.8), new Point(100.2, 0.2))))));

}

@Test // GH-4950
void shouldSerializeJsonPointCorrectly() throws IOException {

String json = "{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}";

assertThat(mapper.writeValueAsString(new GeoJsonPoint(10D, 20D))).isEqualTo(json);
}

@Test // GH-4950
void shouldSerializeGeoJsonLineStringCorrectly()
throws IOException {

String json = "{\"type\":\"LineString\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";

assertThat(mapper.writeValueAsString(new GeoJsonLineString(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)))))
.isEqualTo(json);
}

@Test // GH-4950
void shouldSerializeGeoJsonMultiPointCorrectly()
throws IOException {

String json = "{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";

assertThat(mapper.writeValueAsString(new GeoJsonMultiPoint(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)))))
.isEqualTo(json);
}

@Test // GH-4950
@SuppressWarnings("unchecked")
void shouldSerializeGeoJsonMultiLineStringCorrectly()
throws IOException {

String json = "{\"type\":\"MultiLineString\",\"coordinates\":[[[10.0,20.0],[30.0,40.0]],[[50.0,60.0],[70.0,80.0]]]}";

assertThat(mapper.writeValueAsString(new GeoJsonMultiLineString(
Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80)))))
.isEqualTo(json);
}

@Test // GH-4950
void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {

String json = "{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}";

assertThat(mapper.writeValueAsString(new GeoJsonPolygon(
Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)))))
.isEqualTo(json);
}

@Test // GH-4950
void shouldSerializeGeoJsonMultiPolygonCorrectly()
throws IOException {

String json="{\"type\":\"MultiPolygon\",\"coordinates\":["
+"[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]],"
+"[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]],"
+"[[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]"//
+"]}";

assertThat(mapper.writeValueAsString(new GeoJsonMultiPolygon(Arrays.asList(
new GeoJsonPolygon(Arrays.asList(new Point(102, 2), new Point(103, 2), new Point(103, 3), new Point(102, 3),
new Point(102, 2))),
new GeoJsonPolygon(Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1),
new Point(100, 0))),
new GeoJsonPolygon(Arrays.asList(new Point(100.2, 0.2), new Point(100.8, 0.2), new Point(100.8, 0.8),
new Point(100.2, 0.8), new Point(100.2, 0.2))))))).isEqualTo(json);

}
}