Skip to content

Commit 9f1768c

Browse files
committed
Serializers in GeoJsonModule constructor.
Resolves spring-projects#4950
1 parent 9496d1b commit 9f1768c

File tree

2 files changed

+101
-32
lines changed

2 files changed

+101
-32
lines changed

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,31 @@
3434
import com.fasterxml.jackson.databind.node.ArrayNode;
3535

3636
/**
37-
* A Jackson {@link Module} to register custom {@link JsonDeserializer}s for GeoJSON types.
37+
* A Jackson {@link Module} to register custom {@link JsonDeserializer}s and {@link JsonSerializer}s for GeoJSON types.
3838
* <br />
3939
* Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and
4040
* {@link JsonDeserializer deserializers}.
4141
*
4242
* @author Christoph Strobl
4343
* @author Oliver Gierke
4444
* @author Mark Paluch
45+
* @author Artyom Muravlev
4546
* @since 1.7
4647
*/
4748
public class GeoJsonModule extends SimpleModule {
4849

4950
private static final long serialVersionUID = -8723016728655643720L;
51+
private static final Version VERSION = new Version(3,
52+
2,
53+
0,
54+
null,
55+
"org.springframework.data",
56+
"spring-data-mongodb-geojson");
5057

5158
public GeoJsonModule() {
52-
59+
super("Spring Data MongoDB GeoJson", Version.unknownVersion());
5360
registerDeserializersIn(this);
54-
// TODO: add serializers as of next major version (4.0).
61+
GeoJsonSerializersModule.registerSerializersIn(this);
5562
}
5663

5764
/**
@@ -71,7 +78,7 @@ public GeoJsonModule() {
7178
public static Module deserializers() {
7279

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

95102
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Serializers",
96-
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
103+
VERSION);
97104
GeoJsonSerializersModule.registerSerializersIn(module);
98105
return module;
99106
}
@@ -115,12 +122,7 @@ public static Module serializers() {
115122
* @since 3.2
116123
*/
117124
public static Module geoJsonModule() {
118-
119-
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson",
120-
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
121-
GeoJsonSerializersModule.registerSerializersIn(module);
122-
registerDeserializersIn(module);
123-
return module;
125+
return new GeoJsonModule();
124126
}
125127

126128
private static void registerDeserializersIn(SimpleModule module) {

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

+88-21
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,42 @@
1515
*/
1616
package org.springframework.data.mongodb.core.geo;
1717

18-
import static org.assertj.core.api.Assertions.*;
19-
20-
import java.io.IOException;
21-
import java.util.Arrays;
22-
18+
import com.fasterxml.jackson.databind.ObjectMapper;
2319
import org.junit.jupiter.api.BeforeEach;
2420
import org.junit.jupiter.api.Test;
25-
2621
import org.springframework.data.geo.Point;
2722

28-
import com.fasterxml.jackson.core.JsonParseException;
29-
import com.fasterxml.jackson.databind.JsonMappingException;
30-
import com.fasterxml.jackson.databind.ObjectMapper;
23+
import java.io.IOException;
24+
import java.util.Arrays;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
3127

3228
/**
3329
* @author Christoph Strobl
30+
* @author Artyom Muravlev
3431
*/
35-
public class GeoJsonModuleUnitTests {
32+
class GeoJsonModuleUnitTests {
3633

3734
ObjectMapper mapper;
3835

3936
@BeforeEach
40-
public void setUp() {
37+
void setUp() {
4138

4239
mapper = new ObjectMapper();
4340
mapper.registerModule(new GeoJsonModule());
4441
}
4542

4643
@Test // DATAMONGO-1181
47-
public void shouldDeserializeJsonPointCorrectly() throws JsonParseException, JsonMappingException, IOException {
44+
void shouldDeserializeJsonPointCorrectly() throws IOException {
4845

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

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

5451
@Test // DATAMONGO-1181
55-
public void shouldDeserializeGeoJsonLineStringCorrectly()
56-
throws JsonParseException, JsonMappingException, IOException {
52+
void shouldDeserializeGeoJsonLineStringCorrectly()
53+
throws IOException {
5754

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

@@ -62,8 +59,8 @@ public void shouldDeserializeGeoJsonLineStringCorrectly()
6259
}
6360

6461
@Test // DATAMONGO-1181
65-
public void shouldDeserializeGeoJsonMultiPointCorrectly()
66-
throws JsonParseException, JsonMappingException, IOException {
62+
void shouldDeserializeGeoJsonMultiPointCorrectly()
63+
throws IOException {
6764

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

@@ -73,8 +70,8 @@ public void shouldDeserializeGeoJsonMultiPointCorrectly()
7370

7471
@Test // DATAMONGO-1181
7572
@SuppressWarnings("unchecked")
76-
public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
77-
throws JsonParseException, JsonMappingException, IOException {
73+
void shouldDeserializeGeoJsonMultiLineStringCorrectly()
74+
throws IOException {
7875

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

@@ -83,7 +80,7 @@ public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
8380
}
8481

8582
@Test // DATAMONGO-1181
86-
public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException, JsonMappingException, IOException {
83+
void shouldDeserializeGeoJsonPolygonCorrectly() throws IOException {
8784

8885
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] ] ]}";
8986

@@ -92,8 +89,8 @@ public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException
9289
}
9390

9491
@Test // DATAMONGO-1181
95-
public void shouldDeserializeGeoJsonMultiPolygonCorrectly()
96-
throws JsonParseException, JsonMappingException, IOException {
92+
void shouldDeserializeGeoJsonMultiPolygonCorrectly()
93+
throws IOException {
9794

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

112109
}
110+
111+
@Test // GH-4950
112+
void shouldSerializeJsonPointCorrectly() throws IOException {
113+
114+
String json = "{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}";
115+
116+
assertThat(mapper.writeValueAsString(new GeoJsonPoint(10D, 20D))).isEqualTo(json);
117+
}
118+
119+
@Test // GH-4950
120+
void shouldSerializeGeoJsonLineStringCorrectly()
121+
throws IOException {
122+
123+
String json = "{\"type\":\"LineString\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";
124+
125+
assertThat(mapper.writeValueAsString(new GeoJsonLineString(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)))))
126+
.isEqualTo(json);
127+
}
128+
129+
@Test // GH-4950
130+
void shouldSerializeGeoJsonMultiPointCorrectly()
131+
throws IOException {
132+
133+
String json = "{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";
134+
135+
assertThat(mapper.writeValueAsString(new GeoJsonMultiPoint(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)))))
136+
.isEqualTo(json);
137+
}
138+
139+
@Test // GH-4950
140+
@SuppressWarnings("unchecked")
141+
void shouldSerializeGeoJsonMultiLineStringCorrectly()
142+
throws IOException {
143+
144+
String json = "{\"type\":\"MultiLineString\",\"coordinates\":[[[10.0,20.0],[30.0,40.0]],[[50.0,60.0],[70.0,80.0]]]}";
145+
146+
assertThat(mapper.writeValueAsString(new GeoJsonMultiLineString(
147+
Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80)))))
148+
.isEqualTo(json);
149+
}
150+
151+
@Test // GH-4950
152+
void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
153+
154+
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]]]}";
155+
156+
assertThat(mapper.writeValueAsString(new GeoJsonPolygon(
157+
Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)))))
158+
.isEqualTo(json);
159+
}
160+
161+
@Test // GH-4950
162+
void shouldSerializeGeoJsonMultiPolygonCorrectly()
163+
throws IOException {
164+
165+
String json="{\"type\":\"MultiPolygon\",\"coordinates\":["
166+
+"[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]],"
167+
+"[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]],"
168+
+"[[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]"//
169+
+"]}";
170+
171+
assertThat(mapper.writeValueAsString(new GeoJsonMultiPolygon(Arrays.asList(
172+
new GeoJsonPolygon(Arrays.asList(new Point(102, 2), new Point(103, 2), new Point(103, 3), new Point(102, 3),
173+
new Point(102, 2))),
174+
new GeoJsonPolygon(Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1),
175+
new Point(100, 0))),
176+
new GeoJsonPolygon(Arrays.asList(new Point(100.2, 0.2), new Point(100.8, 0.2), new Point(100.8, 0.8),
177+
new Point(100.2, 0.8), new Point(100.2, 0.2))))))).isEqualTo(json);
178+
179+
}
113180
}

0 commit comments

Comments
 (0)