Skip to content

Commit 7eed824

Browse files
committed
spring-projects#4950 Serializers in GeoJsonModule constructor.
1 parent 9496d1b commit 7eed824

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

spring-data-mongodb/pom.xml

+2
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@
418418
<exclude>**/PerformanceTests.java</exclude>
419419
<exclude>**/ReactivePerformanceTests.java</exclude>
420420
</excludes>
421+
<parallel>classes</parallel>
422+
<threadCount>4</threadCount>
421423
<systemPropertyVariables>
422424
<mongo.client>${mongo}</mongo.client>
423425
<mongo.server>${env.MONGO_VERSION}</mongo.server>

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

+12-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
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}.
@@ -47,11 +47,17 @@
4747
public class GeoJsonModule extends SimpleModule {
4848

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

5157
public GeoJsonModule() {
52-
58+
super("Spring Data MongoDB GeoJson", Version.unknownVersion());
5359
registerDeserializersIn(this);
54-
// TODO: add serializers as of next major version (4.0).
60+
GeoJsonSerializersModule.registerSerializersIn(this);
5561
}
5662

5763
/**
@@ -71,7 +77,7 @@ public GeoJsonModule() {
7177
public static Module deserializers() {
7278

7379
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Deserializers",
74-
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
80+
VERSION);
7581
registerDeserializersIn(module);
7682
return module;
7783
}
@@ -93,7 +99,7 @@ public static Module deserializers() {
9399
public static Module serializers() {
94100

95101
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Serializers",
96-
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
102+
VERSION);
97103
GeoJsonSerializersModule.registerSerializersIn(module);
98104
return module;
99105
}
@@ -115,12 +121,7 @@ public static Module serializers() {
115121
* @since 3.2
116122
*/
117123
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;
124+
return new GeoJsonModule();
124125
}
125126

126127
private static void registerDeserializersIn(SimpleModule module) {

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

+82-12
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@
3232
/**
3333
* @author Christoph Strobl
3434
*/
35-
public class GeoJsonModuleUnitTests {
35+
class GeoJsonModuleUnitTests {
3636

3737
ObjectMapper mapper;
3838

3939
@BeforeEach
40-
public void setUp() {
40+
void setUp() {
4141

4242
mapper = new ObjectMapper();
4343
mapper.registerModule(new GeoJsonModule());
4444
}
4545

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

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

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

5454
@Test // DATAMONGO-1181
55-
public void shouldDeserializeGeoJsonLineStringCorrectly()
56-
throws JsonParseException, JsonMappingException, IOException {
55+
void shouldDeserializeGeoJsonLineStringCorrectly()
56+
throws IOException {
5757

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

@@ -62,8 +62,8 @@ public void shouldDeserializeGeoJsonLineStringCorrectly()
6262
}
6363

6464
@Test // DATAMONGO-1181
65-
public void shouldDeserializeGeoJsonMultiPointCorrectly()
66-
throws JsonParseException, JsonMappingException, IOException {
65+
void shouldDeserializeGeoJsonMultiPointCorrectly()
66+
throws IOException {
6767

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

@@ -73,8 +73,8 @@ public void shouldDeserializeGeoJsonMultiPointCorrectly()
7373

7474
@Test // DATAMONGO-1181
7575
@SuppressWarnings("unchecked")
76-
public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
77-
throws JsonParseException, JsonMappingException, IOException {
76+
void shouldDeserializeGeoJsonMultiLineStringCorrectly()
77+
throws IOException {
7878

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

@@ -83,7 +83,7 @@ public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
8383
}
8484

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

8888
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] ] ]}";
8989

@@ -92,8 +92,8 @@ public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException
9292
}
9393

9494
@Test // DATAMONGO-1181
95-
public void shouldDeserializeGeoJsonMultiPolygonCorrectly()
96-
throws JsonParseException, JsonMappingException, IOException {
95+
void shouldDeserializeGeoJsonMultiPolygonCorrectly()
96+
throws IOException {
9797

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

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

0 commit comments

Comments
 (0)