From 0bcdd58f042bd9ca52c358e64a1b57aec6fad0e6 Mon Sep 17 00:00:00 2001 From: muravlevas Date: Tue, 22 Apr 2025 19:05:52 +0700 Subject: [PATCH] Serializers in GeoJsonModule constructor. Resolves #4950 Signed-off-by: muravlevas --- .../data/mongodb/core/geo/GeoJsonModule.java | 24 ++--- .../core/geo/GeoJsonModuleUnitTests.java | 97 ++++++++++++++++--- 2 files changed, 96 insertions(+), 25 deletions(-) diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java index bc74a56df3..0114cf2b17 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/geo/GeoJsonModule.java @@ -34,7 +34,7 @@ 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. *
* Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and * {@link JsonDeserializer deserializers}. @@ -42,16 +42,23 @@ * @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); } /** @@ -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; } @@ -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; } @@ -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) { diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java index e65101177c..7b8b90a35f 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/geo/GeoJsonModuleUnitTests.java @@ -25,26 +25,25 @@ 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 void shouldDeserializeJsonPointCorrectly() throws JsonParseException, Jso } @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 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] ]}"; @@ -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] ] ]}"; @@ -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] ] ]}"; @@ -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]]]," @@ -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); + + } }