Skip to content

Commit cba9088

Browse files
bjornharvoldmp911de
authored andcommitted
Added a serializers module to all GeoJson types with unit tests.
Original pull request: #3539. Closes #3517
1 parent a33aece commit cba9088

File tree

2 files changed

+307
-0
lines changed

2 files changed

+307
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* Copyright 2015-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
import com.fasterxml.jackson.core.JsonGenerator;
19+
import com.fasterxml.jackson.core.JsonProcessingException;
20+
import com.fasterxml.jackson.databind.JsonSerializer;
21+
import com.fasterxml.jackson.databind.Module;
22+
import com.fasterxml.jackson.databind.SerializerProvider;
23+
import com.fasterxml.jackson.databind.module.SimpleModule;
24+
import org.springframework.data.geo.Point;
25+
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
/**
31+
* A Jackson {@link Module} to register custom {@link JsonSerializer}s for GeoJSON types.
32+
*
33+
* @author Bjorn Harvold
34+
* @since
35+
*/
36+
public class GeoJsonSerializersModule extends SimpleModule {
37+
38+
private static final long serialVersionUID = 1340494654898895610L;
39+
40+
public GeoJsonSerializersModule() {
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());
47+
}
48+
49+
public static class GeoJsonPointSerializer extends JsonSerializer<GeoJsonPoint> {
50+
@Override
51+
public void serialize(GeoJsonPoint value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
52+
gen.writeStartObject();
53+
gen.writeStringField("type", value.getType());
54+
gen.writeObjectField("coordinates", value.getCoordinates());
55+
gen.writeEndObject();
56+
}
57+
58+
}
59+
60+
public static class GeoJsonLineStringSerializer extends JsonSerializer<GeoJsonLineString> {
61+
62+
@Override
63+
public void serialize(GeoJsonLineString value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
64+
gen.writeStartObject();
65+
gen.writeStringField("type", value.getType());
66+
gen.writeArrayFieldStart("coordinates");
67+
for (Point p : value.getCoordinates()) {
68+
gen.writeObject(new double[]{p.getX(), p.getY()});
69+
}
70+
gen.writeEndArray();
71+
gen.writeEndObject();
72+
}
73+
}
74+
75+
public static class GeoJsonMultiPointSerializer extends JsonSerializer<GeoJsonMultiPoint> {
76+
77+
@Override
78+
public void serialize(GeoJsonMultiPoint value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
79+
gen.writeStartObject();
80+
gen.writeStringField("type", value.getType());
81+
gen.writeArrayFieldStart("coordinates");
82+
for (Point p : value.getCoordinates()) {
83+
gen.writeObject(new double[]{p.getX(), p.getY()});
84+
}
85+
gen.writeEndArray();
86+
gen.writeEndObject();
87+
}
88+
}
89+
90+
public static class GeoJsonMultiLineStringSerializer extends JsonSerializer<GeoJsonMultiLineString> {
91+
92+
@Override
93+
public void serialize(GeoJsonMultiLineString value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
94+
gen.writeStartObject();
95+
gen.writeStringField("type", value.getType());
96+
gen.writeArrayFieldStart("coordinates");
97+
for (GeoJsonLineString lineString : value.getCoordinates()) {
98+
List<double[]> arrayList = new ArrayList<>();
99+
for (Point p : lineString.getCoordinates()) {
100+
arrayList.add(new double[]{p.getX(), p.getY()});
101+
}
102+
double[][] doubles = arrayList.toArray(new double[0][0]);
103+
gen.writeObject(doubles);
104+
}
105+
gen.writeEndArray();
106+
gen.writeEndObject();
107+
}
108+
}
109+
110+
public static class GeoJsonPolygonSerializer extends JsonSerializer<GeoJsonPolygon> {
111+
112+
@Override
113+
public void serialize(GeoJsonPolygon value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
114+
gen.writeStartObject();
115+
gen.writeStringField("type", value.getType());
116+
gen.writeArrayFieldStart("coordinates");
117+
for (GeoJsonLineString ls : value.getCoordinates()) {
118+
gen.writeStartArray();
119+
for (Point p : ls.getCoordinates()) {
120+
gen.writeObject(new double[]{p.getX(), p.getY()});
121+
}
122+
gen.writeEndArray();
123+
}
124+
gen.writeEndArray();
125+
gen.writeEndObject();
126+
}
127+
}
128+
129+
public static class GeoJsonMultiPolygonSerializer extends JsonSerializer<GeoJsonMultiPolygon> {
130+
131+
@Override
132+
public void serialize(GeoJsonMultiPolygon value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
133+
gen.writeStartObject();
134+
gen.writeStringField("type", value.getType());
135+
gen.writeArrayFieldStart("coordinates");
136+
for (GeoJsonPolygon polygon : value.getCoordinates()) {
137+
138+
gen.writeStartArray();
139+
140+
gen.writeStartArray();
141+
for (GeoJsonLineString lineString : polygon.getCoordinates()) {
142+
143+
for (Point p : lineString.getCoordinates()) {
144+
gen.writeObject(new double[]{p.getX(), p.getY()});
145+
}
146+
147+
}
148+
149+
gen.writeEndArray();
150+
gen.writeEndArray();
151+
}
152+
gen.writeEndArray();
153+
gen.writeEndObject();
154+
}
155+
}
156+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/*
2+
* Copyright 2015-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.geo;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import org.junit.jupiter.api.BeforeEach;
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.data.geo.Point;
22+
23+
import java.io.IOException;
24+
import java.util.Arrays;
25+
import java.util.List;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* @author Bjorn Harvold
31+
*/
32+
public class GeoJsonModuleRoundTripUnitTests {
33+
34+
ObjectMapper mapper;
35+
36+
@BeforeEach
37+
public void setUp() {
38+
39+
mapper = new ObjectMapper();
40+
mapper.registerModule(new GeoJsonModule());
41+
mapper.registerModule(new GeoJsonSerializersModule());
42+
}
43+
44+
@Test
45+
public void shouldMarshalJsonPointCorrectly() throws IOException {
46+
47+
String json = "{\"type\":\"Point\",\"coordinates\":[10.0,20.0]}";
48+
GeoJsonPoint point = new GeoJsonPoint(10D, 20D);
49+
50+
assertThat(mapper.readValue(json, GeoJsonPoint.class)).isEqualTo(point);
51+
52+
String backToJSON = mapper.writeValueAsString(point);
53+
54+
assertThat(backToJSON).isEqualTo(json);
55+
}
56+
57+
@Test
58+
public void shouldMarshalGeoJsonLineStringCorrectly()
59+
throws IOException {
60+
61+
String json = "{\"type\":\"LineString\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";
62+
63+
GeoJsonLineString lineString = new GeoJsonLineString(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)));
64+
65+
assertThat(mapper.readValue(json, GeoJsonLineString.class)).isEqualTo(lineString);
66+
67+
String backToJSON = mapper.writeValueAsString(lineString);
68+
69+
assertThat(backToJSON).isEqualTo(json);
70+
}
71+
72+
@Test
73+
public void shouldMarshalGeoJsonMultiPointCorrectly() throws IOException {
74+
75+
String json = "{\"type\":\"MultiPoint\",\"coordinates\":[[10.0,20.0],[30.0,40.0],[50.0,60.0]]}";
76+
GeoJsonMultiPoint multiPoint = new GeoJsonMultiPoint(Arrays.asList(new Point(10, 20), new Point(30, 40), new Point(50, 60)));
77+
78+
assertThat(mapper.readValue(json, GeoJsonMultiPoint.class)).isEqualTo(multiPoint);
79+
80+
String backToJSON = mapper.writeValueAsString(multiPoint);
81+
82+
assertThat(backToJSON).isEqualTo(json);
83+
}
84+
85+
@Test
86+
@SuppressWarnings("unchecked")
87+
public void shouldMarshalGeoJsonMultiLineStringCorrectly() throws IOException {
88+
89+
String json = "{\"type\":\"MultiLineString\",\"coordinates\":[[[10.0,20.0],[30.0,40.0]],[[50.0,60.0],[70.0,80.0]]]}";
90+
GeoJsonMultiLineString multiLineString = new GeoJsonMultiLineString(Arrays.asList(new Point(10, 20), new Point(30, 40)), Arrays.asList(new Point(50, 60), new Point(70, 80)));
91+
92+
assertThat(mapper.readValue(json, GeoJsonMultiLineString.class)).isEqualTo(multiLineString);
93+
94+
String backToJSON = mapper.writeValueAsString(multiLineString);
95+
96+
assertThat(backToJSON).isEqualTo(json);
97+
}
98+
99+
@Test
100+
public void shouldMarshalGeoJsonPolygonCorrectly() throws IOException {
101+
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]]]}";
102+
103+
List<Point> points = Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0));
104+
GeoJsonPolygon polygon = new GeoJsonPolygon(points);
105+
106+
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(polygon);
107+
108+
String backToJSON = mapper.writeValueAsString(polygon);
109+
110+
assertThat(backToJSON).isEqualTo(json);
111+
}
112+
113+
@Test
114+
public void shouldMarshalGeoJsonMultiPolygonCorrectly()
115+
throws IOException {
116+
117+
String json = "{\"type\":\"MultiPolygon\",\"coordinates\":["
118+
+ "["
119+
+ "["
120+
+ "[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]"
121+
+ "]"
122+
+ "],"
123+
+ "["
124+
+ "["
125+
+ "[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]"
126+
+ "]"
127+
+ "],"
128+
+ "["
129+
+ "["
130+
+ "[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]"
131+
+ "]"
132+
+ "]"
133+
+ "]"
134+
+ "}";
135+
136+
GeoJsonMultiPolygon multiPolygon = new GeoJsonMultiPolygon(Arrays.asList(
137+
new GeoJsonPolygon(Arrays.asList(new Point(102, 2), new Point(103, 2), new Point(103, 3), new Point(102, 3),
138+
new Point(102, 2))),
139+
new GeoJsonPolygon(Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1),
140+
new Point(100, 0))),
141+
new GeoJsonPolygon(Arrays.asList(new Point(100.2, 0.2), new Point(100.8, 0.2), new Point(100.8, 0.8),
142+
new Point(100.2, 0.8), new Point(100.2, 0.2)))));
143+
144+
assertThat(mapper.readValue(json, GeoJsonMultiPolygon.class)).isEqualTo(multiPolygon);
145+
146+
String backToJSON = mapper.writeValueAsString(multiPolygon);
147+
148+
assertThat(backToJSON).isEqualTo(json);
149+
150+
}
151+
}

0 commit comments

Comments
 (0)