Skip to content

Commit c1417c4

Browse files
committed
Polishing.
Move registerSerializersIn(…) entirely to GeoJsonSerializersModule. Tweak Javadoc and wording. Original pull request: #3539. Closes #3517
1 parent 36515ab commit c1417c4

File tree

4 files changed

+34
-38
lines changed

4 files changed

+34
-38
lines changed

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

+7-23
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
import java.util.List;
2222

2323
import org.springframework.data.geo.Point;
24-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonLineStringSerializer;
25-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiLineStringSerializer;
26-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiPointSerializer;
27-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonMultiPolygonSerializer;
28-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonPointSerializer;
29-
import org.springframework.data.mongodb.core.geo.GeoJsonSerializersModule.GeoJsonPolygonSerializer;
3024
import org.springframework.lang.Nullable;
3125

3226
import com.fasterxml.jackson.core.JsonParser;
@@ -70,7 +64,7 @@ public GeoJsonModule() {
7064
* <li>{@link GeoJsonPolygon}</li>
7165
* <li>{@link GeoJsonMultiPolygon}</li>
7266
* </ul>
73-
*
67+
*
7468
* @return a {@link Module} containing {@link JsonDeserializer deserializers} for {@link GeoJson} types.
7569
* @since 3.2
7670
*/
@@ -100,7 +94,7 @@ public static Module serializers() {
10094

10195
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson - Serializers",
10296
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
103-
registerSerializersIn(module);
97+
GeoJsonSerializersModule.registerSerializersIn(module);
10498
return module;
10599
}
106100

@@ -124,21 +118,11 @@ public static Module geoJsonModule() {
124118

125119
SimpleModule module = new SimpleModule("Spring Data MongoDB GeoJson",
126120
new Version(3, 2, 0, null, "org.springframework.data", "spring-data-mongodb-geojson"));
127-
registerSerializersIn(module);
121+
GeoJsonSerializersModule.registerSerializersIn(module);
128122
registerDeserializersIn(module);
129123
return module;
130124
}
131125

132-
private static void registerSerializersIn(SimpleModule module) {
133-
134-
module.addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer());
135-
module.addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer());
136-
module.addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer());
137-
module.addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer());
138-
module.addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer());
139-
module.addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer());
140-
}
141-
142126
private static void registerDeserializersIn(SimpleModule module) {
143127

144128
module.addDeserializer(GeoJsonPoint.class, new GeoJsonPointDeserializer());
@@ -227,7 +211,7 @@ protected List<Point> toPoints(@Nullable ArrayNode node) {
227211
return Collections.emptyList();
228212
}
229213

230-
List<Point> points = new ArrayList<Point>(node.size());
214+
List<Point> points = new ArrayList<>(node.size());
231215

232216
for (JsonNode coordinatePair : node) {
233217
if (coordinatePair.isArray()) {
@@ -238,7 +222,7 @@ protected List<Point> toPoints(@Nullable ArrayNode node) {
238222
}
239223

240224
protected GeoJsonLineString toLineString(ArrayNode node) {
241-
return new GeoJsonLineString(toPoints((ArrayNode) node));
225+
return new GeoJsonLineString(toPoints(node));
242226
}
243227
}
244228

@@ -352,7 +336,7 @@ private static class GeoJsonMultiLineStringDeserializer extends GeoJsonDeseriali
352336
@Override
353337
protected GeoJsonMultiLineString doDeserialize(ArrayNode coordinates) {
354338

355-
List<GeoJsonLineString> lines = new ArrayList<GeoJsonLineString>(coordinates.size());
339+
List<GeoJsonLineString> lines = new ArrayList<>(coordinates.size());
356340

357341
for (JsonNode lineString : coordinates) {
358342
if (lineString.isArray()) {
@@ -429,7 +413,7 @@ private static class GeoJsonMultiPolygonDeserializer extends GeoJsonDeserializer
429413
@Override
430414
protected GeoJsonMultiPolygon doDeserialize(ArrayNode coordinates) {
431415

432-
List<GeoJsonPolygon> polygones = new ArrayList<GeoJsonPolygon>(coordinates.size());
416+
List<GeoJsonPolygon> polygones = new ArrayList<>(coordinates.size());
433417

434418
for (JsonNode polygon : coordinates) {
435419
for (JsonNode ring : polygon) {

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

+11-6
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,18 @@ class GeoJsonSerializersModule extends SimpleModule {
3737
private static final long serialVersionUID = 1340494654898895610L;
3838

3939
GeoJsonSerializersModule() {
40+
registerSerializersIn(this);
41+
}
42+
43+
44+
static void registerSerializersIn(SimpleModule module) {
4045

41-
addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer());
42-
addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer());
43-
addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer());
44-
addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer());
45-
addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer());
46-
addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer());
46+
module.addSerializer(GeoJsonPoint.class, new GeoJsonPointSerializer());
47+
module.addSerializer(GeoJsonMultiPoint.class, new GeoJsonMultiPointSerializer());
48+
module.addSerializer(GeoJsonLineString.class, new GeoJsonLineStringSerializer());
49+
module.addSerializer(GeoJsonMultiLineString.class, new GeoJsonMultiLineStringSerializer());
50+
module.addSerializer(GeoJsonPolygon.class, new GeoJsonPolygonSerializer());
51+
module.addSerializer(GeoJsonMultiPolygon.class, new GeoJsonMultiPolygonSerializer());
4752
}
4853

4954
/**

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

+10-7
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@
2828
import com.fasterxml.jackson.databind.ObjectMapper;
2929

3030
/**
31+
* Unit tests for {@link GeoJsonSerializersModule}.
32+
*
3133
* @author Bjorn Harvold
3234
* @author Christoph Strobl
3335
*/
3436
class GeoJsonSerializersUnitTests {
3537

36-
ObjectMapper mapper;
38+
private ObjectMapper mapper;
3739

3840
@BeforeEach
3941
void beforeEach() {
@@ -51,7 +53,7 @@ void shouldSerializeJsonPointCorrectly() throws IOException {
5153
}
5254

5355
@Test // GH-3517
54-
public void shouldSerializeGeoJsonLineStringCorrectly() throws IOException {
56+
void shouldSerializeGeoJsonLineStringCorrectly() throws IOException {
5557

5658
GeoJsonLineString lineString = new GeoJsonLineString(
5759
Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)));
@@ -61,7 +63,7 @@ public void shouldSerializeGeoJsonLineStringCorrectly() throws IOException {
6163
}
6264

6365
@Test // GH-3517
64-
public void shouldSerializeGeoJsonMultiPointCorrectly() throws IOException {
66+
void shouldSerializeGeoJsonMultiPointCorrectly() throws IOException {
6567

6668
GeoJsonMultiPoint multiPoint = new GeoJsonMultiPoint(
6769
Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)));
@@ -71,7 +73,7 @@ public void shouldSerializeGeoJsonMultiPointCorrectly() throws IOException {
7173
}
7274

7375
@Test // GH-3517
74-
public void shouldSerializeJsonMultiLineStringCorrectly() throws IOException {
76+
void shouldSerializeJsonMultiLineStringCorrectly() throws IOException {
7577

7678
GeoJsonMultiLineString multiLineString = new GeoJsonMultiLineString(
7779
Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80)));
@@ -81,17 +83,18 @@ public void shouldSerializeJsonMultiLineStringCorrectly() throws IOException {
8183
}
8284

8385
@Test // GH-3517
84-
public void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
86+
void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
8587

8688
List<Point> points = Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1),
8789
new Point(100, 0));
8890
GeoJsonPolygon polygon = new GeoJsonPolygon(points);
8991

90-
assertThat(mapper.writeValueAsString(polygon)).isEqualTo("{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}");
92+
assertThat(mapper.writeValueAsString(polygon)).isEqualTo(
93+
"{\"type\":\"Polygon\",\"coordinates\":[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}");
9194
}
9295

9396
@Test // GH-3517
94-
public void shouldSerializeGeoJsonMultiPolygonCorrectly() throws IOException {
97+
void shouldSerializeGeoJsonMultiPolygonCorrectly() throws IOException {
9598

9699
String json = "{\"type\":\"MultiPolygon\",\"coordinates\":[" + "[" + "["
97100
+ "[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]" + "]" + "]," + "[" + "["

src/main/asciidoc/reference/mongodb.adoc

+6-2
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,7 @@ The geo-near operations return a `GeoResults` wrapper object that encapsulates `
14461446

14471447
MongoDB supports https://geojson.org/[GeoJSON] and simple (legacy) coordinate pairs for geospatial data. Those formats can both be used for storing as well as querying data. See the https://docs.mongodb.org/manual/core/2dsphere/#geospatial-indexes-store-geojson/[MongoDB manual on GeoJSON support] to learn about requirements and restrictions.
14481448

1449+
[[mongo.geo-json.domain.classes]]
14491450
==== GeoJSON Types in Domain Classes
14501451

14511452
Usage of https://geojson.org/[GeoJSON] types in domain classes is straightforward. The `org.springframework.data.mongodb.core.geo` package contains types such as `GeoJsonPoint`, `GeoJsonPolygon`, and others. These types are extend the existing `org.springframework.data.geo` types. The following example uses a `GeoJsonPoint`:
@@ -1469,6 +1470,7 @@ public class Store {
14691470
----
14701471
====
14711472

1473+
[[mongo.geo-json.query-methods]]
14721474
==== GeoJSON Types in Repository Query Methods
14731475

14741476
Using GeoJSON types as repository query parameters forces usage of the `$geometry` operator when creating the query, as the following example shows:
@@ -1529,6 +1531,7 @@ repo.findByLocationWithin( <4>
15291531
<4> Use the legacy format `$polygon` operator.
15301532
====
15311533

1534+
[[mongo.geo-json.metrics]]
15321535
==== Metrics and Distance calculation
15331536

15341537
Then MongoDB `$geoNear` operator allows usage of a GeoJSON Point or legacy coordinate pairs.
@@ -1700,9 +1703,10 @@ Returning the 3 Documents just like the GeoJSON variant:
17001703
<4> Distance from center point in _Kilometers_ - take it times 1000 to match _Meters_ of the GeoJSON variant.
17011704
====
17021705

1706+
[[mongo.geo-json.jackson-modules]]
17031707
==== GeoJSON Jackson Modules
17041708

1705-
By using the <<core.web>>, Spring Data adds additional Jackson ``Modules``s to the `ObjectMapper` for de-/serializing common types used by the Spring Data domain.
1709+
By using the <<core.web>>, Spring Data registers additional Jackson ``Modules``s to the `ObjectMapper` for de-/serializing common Spring Data domain types.
17061710
Please refer to the <<core.web.basic.jackson-mappers>> section to learn more about the infrastructure setup of this feature.
17071711

17081712
The MongoDB module additionally registers ``JsonDeserializer``s for the following GeoJSON types via its `GeoJsonConfiguration` exposing the `GeoJsonModule`.
@@ -1734,7 +1738,7 @@ class GeoJsonConfiguration implements SpringDataJacksonModules {
17341738

17351739
[WARNING]
17361740
====
1737-
The next major version (`4.0`) will by default register both, ``JsonDeserializer``s and ``JsonSerializer``s for GeoJSON types.
1741+
The next major version (`4.0`) will register both, ``JsonDeserializer``s and ``JsonSerializer``s for GeoJSON types by default.
17381742
====
17391743

17401744
[[mongo.textsearch]]

0 commit comments

Comments
 (0)