Browse Source

Merge 0bcdd58f04 into 002ca6a2b9

pull/4955/merge
muravlevas 1 day ago committed by GitHub
parent
commit
b26ddba6d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java
  2. 97
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java

24
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java

@ -35,7 +35,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule; @@ -35,7 +35,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
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}.
@ -43,16 +43,23 @@ import com.fasterxml.jackson.databind.node.ArrayNode; @@ -43,16 +43,23 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
* @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);
}
/**
@ -72,7 +79,7 @@ public class GeoJsonModule extends SimpleModule { @@ -72,7 +79,7 @@ public class GeoJsonModule extends SimpleModule {
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;
}
@ -94,7 +101,7 @@ public class GeoJsonModule extends SimpleModule { @@ -94,7 +101,7 @@ public class GeoJsonModule extends SimpleModule {
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;
}
@ -116,12 +123,7 @@ public class GeoJsonModule extends SimpleModule { @@ -116,12 +123,7 @@ public class GeoJsonModule extends SimpleModule {
* @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) {

97
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java

@ -25,26 +25,25 @@ import org.junit.jupiter.api.Test; @@ -25,26 +25,25 @@ import org.junit.jupiter.api.Test;
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] }";
@ -52,8 +51,8 @@ public class GeoJsonModuleUnitTests { @@ -52,8 +51,8 @@ public class GeoJsonModuleUnitTests {
}
@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] ]}";
@ -62,8 +61,8 @@ public class GeoJsonModuleUnitTests { @@ -62,8 +61,8 @@ public class GeoJsonModuleUnitTests {
}
@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] ]}";
@ -73,8 +72,8 @@ public class GeoJsonModuleUnitTests { @@ -73,8 +72,8 @@ public class GeoJsonModuleUnitTests {
@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] ] ]}";
@ -83,7 +82,7 @@ public class GeoJsonModuleUnitTests { @@ -83,7 +82,7 @@ public class GeoJsonModuleUnitTests {
}
@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] ] ]}";
@ -92,8 +91,8 @@ public class GeoJsonModuleUnitTests { @@ -92,8 +91,8 @@ public class GeoJsonModuleUnitTests {
}
@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]]],"
@ -110,4 +109,74 @@ public class GeoJsonModuleUnitTests { @@ -110,4 +109,74 @@ public class GeoJsonModuleUnitTests {
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);
}
}

Loading…
Cancel
Save