Skip to content

Commit 0bcdd58

Browse files
committed
Serializers in GeoJsonModule constructor.
Resolves spring-projects#4950 Signed-off-by: muravlevas <[email protected]>
1 parent 9496d1b commit 0bcdd58

File tree

2 files changed

+96
-25
lines changed

2 files changed

+96
-25
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

+83-14
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,34 @@
2525

2626
import org.springframework.data.geo.Point;
2727

28-
import com.fasterxml.jackson.core.JsonParseException;
29-
import com.fasterxml.jackson.databind.JsonMappingException;
3028
import com.fasterxml.jackson.databind.ObjectMapper;
3129

3230
/**
3331
* @author Christoph Strobl
32+
* @author Artyom Muravlev
3433
*/
35-
public class GeoJsonModuleUnitTests {
34+
class GeoJsonModuleUnitTests {
3635

3736
ObjectMapper mapper;
3837

3938
@BeforeEach
40-
public void setUp() {
39+
void setUp() {
4140

4241
mapper = new ObjectMapper();
4342
mapper.registerModule(new GeoJsonModule());
4443
}
4544

4645
@Test // DATAMONGO-1181
47-
public void shouldDeserializeJsonPointCorrectly() throws JsonParseException, JsonMappingException, IOException {
46+
void shouldDeserializeJsonPointCorrectly() throws IOException {
4847

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

5150
assertThat(mapper.readValue(json, GeoJsonPoint.class)).isEqualTo(new GeoJsonPoint(10D, 20D));
5251
}
5352

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

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

@@ -62,8 +61,8 @@ public void shouldDeserializeGeoJsonLineStringCorrectly()
6261
}
6362

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

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

@@ -73,8 +72,8 @@ public void shouldDeserializeGeoJsonMultiPointCorrectly()
7372

7473
@Test // DATAMONGO-1181
7574
@SuppressWarnings("unchecked")
76-
public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
77-
throws JsonParseException, JsonMappingException, IOException {
75+
void shouldDeserializeGeoJsonMultiLineStringCorrectly()
76+
throws IOException {
7877

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

@@ -83,7 +82,7 @@ public void shouldDeserializeGeoJsonMultiLineStringCorrectly()
8382
}
8483

8584
@Test // DATAMONGO-1181
86-
public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException, JsonMappingException, IOException {
85+
void shouldDeserializeGeoJsonPolygonCorrectly() throws IOException {
8786

8887
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] ] ]}";
8988

@@ -92,8 +91,8 @@ public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException
9291
}
9392

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

9897
String json = "{ \"type\": \"Polygon\", \"coordinates\": ["
9998
+ "[[[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()
110109
new Point(100.2, 0.8), new Point(100.2, 0.2))))));
111110

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

0 commit comments

Comments
 (0)