Browse Source

Serializers in GeoJsonModule constructor.

Resolves #4950

Signed-off-by: muravlevas <muravlevartem@mail.ru>
pull/4955/head
muravlevas 8 months ago
parent
commit
0bcdd58f04
  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

@ -34,7 +34,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode; 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 /> * <br />
* Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and * Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and
* {@link JsonDeserializer deserializers}. * {@link JsonDeserializer deserializers}.
@ -42,16 +42,23 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
* @author Christoph Strobl * @author Christoph Strobl
* @author Oliver Gierke * @author Oliver Gierke
* @author Mark Paluch * @author Mark Paluch
* @author Artyom Muravlev
* @since 1.7 * @since 1.7
*/ */
public class GeoJsonModule extends SimpleModule { public class GeoJsonModule extends SimpleModule {
private static final long serialVersionUID = -8723016728655643720L; 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() { public GeoJsonModule() {
super("Spring Data MongoDB GeoJson", Version.unknownVersion());
registerDeserializersIn(this); registerDeserializersIn(this);
// TODO: add serializers as of next major version (4.0). GeoJsonSerializersModule.registerSerializersIn(this);
} }
/** /**
@ -71,7 +78,7 @@ public class GeoJsonModule extends SimpleModule {
public static Module deserializers() { public static Module deserializers() {
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - 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); registerDeserializersIn(module);
return module; return module;
} }
@ -93,7 +100,7 @@ public class GeoJsonModule extends SimpleModule {
public static Module serializers() { public static Module serializers() {
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - 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); GeoJsonSerializersModule.registerSerializersIn(module);
return module; return module;
} }
@ -115,12 +122,7 @@ public class GeoJsonModule extends SimpleModule {
* @since 3.2 * @since 3.2
*/ */
public static Module geoJsonModule() { public static Module geoJsonModule() {
return new 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;
} }
private static void registerDeserializersIn(SimpleModule module) { 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;
import org.springframework.data.geo.Point; import org.springframework.data.geo.Point;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* @author Christoph Strobl * @author Christoph Strobl
* @author Artyom Muravlev
*/ */
public class GeoJsonModuleUnitTests { class GeoJsonModuleUnitTests {
ObjectMapper mapper; ObjectMapper mapper;
@BeforeEach @BeforeEach
public void setUp() { void setUp() {
mapper = new ObjectMapper(); mapper = new ObjectMapper();
mapper.registerModule(new GeoJsonModule()); mapper.registerModule(new GeoJsonModule());
} }
@Test // DATAMONGO-1181 @Test // DATAMONGO-1181
public void shouldDeserializeJsonPointCorrectly() throws JsonParseException, JsonMappingException, IOException { void shouldDeserializeJsonPointCorrectly() throws IOException {
String json = "{ \"type\": \"Point\", \"coordinates\": [10.0, 20.0] }"; String json = "{ \"type\": \"Point\", \"coordinates\": [10.0, 20.0] }";
@ -52,8 +51,8 @@ public class GeoJsonModuleUnitTests {
} }
@Test // DATAMONGO-1181 @Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonLineStringCorrectly() void shouldDeserializeGeoJsonLineStringCorrectly()
throws JsonParseException, JsonMappingException, IOException { throws IOException {
String json = "{ \"type\": \"LineString\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}"; String json = "{ \"type\": \"LineString\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}";
@ -62,8 +61,8 @@ public class GeoJsonModuleUnitTests {
} }
@Test // DATAMONGO-1181 @Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonMultiPointCorrectly() void shouldDeserializeGeoJsonMultiPointCorrectly()
throws JsonParseException, JsonMappingException, IOException { throws IOException {
String json = "{ \"type\": \"MultiPoint\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}"; String json = "{ \"type\": \"MultiPoint\", \"coordinates\": [ [10.0, 20.0], [30.0, 40.0], [50.0, 60.0] ]}";
@ -73,8 +72,8 @@ public class GeoJsonModuleUnitTests {
@Test // DATAMONGO-1181 @Test // DATAMONGO-1181
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void shouldDeserializeGeoJsonMultiLineStringCorrectly() void shouldDeserializeGeoJsonMultiLineStringCorrectly()
throws JsonParseException, JsonMappingException, IOException { throws IOException {
String json = "{ \"type\": \"MultiLineString\", \"coordinates\": [ [ [10.0, 20.0], [30.0, 40.0] ], [ [50.0, 60.0] , [70.0, 80.0] ] ]}"; 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 {
} }
@Test // DATAMONGO-1181 @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] ] ]}"; 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 {
} }
@Test // DATAMONGO-1181 @Test // DATAMONGO-1181
public void shouldDeserializeGeoJsonMultiPolygonCorrectly() void shouldDeserializeGeoJsonMultiPolygonCorrectly()
throws JsonParseException, JsonMappingException, IOException { throws IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [" 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]]]," + "[[[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 {
new Point(100.2, 0.8), new Point(100.2, 0.2)))))); 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