2121import java .util .List ;
2222
2323import 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 ;
2430import org .springframework .lang .Nullable ;
2531
2632import com .fasterxml .jackson .core .JsonParser ;
27- import com .fasterxml .jackson .core .JsonProcessingException ;
33+ import com .fasterxml .jackson .core .Version ;
2834import com .fasterxml .jackson .databind .DeserializationContext ;
2935import com .fasterxml .jackson .databind .JsonDeserializer ;
3036import com .fasterxml .jackson .databind .JsonNode ;
3440import com .fasterxml .jackson .databind .node .ArrayNode ;
3541
3642/**
37- * A Jackson {@link Module} to register custom {@link JsonSerializer} and {@link JsonDeserializer}s for GeoJSON types.
43+ * A Jackson {@link Module} to register custom {@link JsonDeserializer}s for GeoJSON types.
44+ * <p />
45+ * Use {@link #geoJsonModule()} to obtain a {@link Module} containing both {@link JsonSerializer serializers} and
46+ * {@link JsonDeserializer deserializers}.
3847 *
3948 * @author Christoph Strobl
4049 * @author Oliver Gierke
@@ -47,12 +56,97 @@ public class GeoJsonModule extends SimpleModule {
4756
4857 public GeoJsonModule () {
4958
50- addDeserializer (GeoJsonPoint .class , new GeoJsonPointDeserializer ());
51- addDeserializer (GeoJsonMultiPoint .class , new GeoJsonMultiPointDeserializer ());
52- addDeserializer (GeoJsonLineString .class , new GeoJsonLineStringDeserializer ());
53- addDeserializer (GeoJsonMultiLineString .class , new GeoJsonMultiLineStringDeserializer ());
54- addDeserializer (GeoJsonPolygon .class , new GeoJsonPolygonDeserializer ());
55- addDeserializer (GeoJsonMultiPolygon .class , new GeoJsonMultiPolygonDeserializer ());
59+ registerDeserializersIn (this );
60+ // TODO: add serializers as of next major version (4.0).
61+ }
62+
63+ /**
64+ * Obtain a {@link Module} containing {@link JsonDeserializer deserializers} for the following {@link GeoJson} types:
65+ * <ul>
66+ * <li>{@link GeoJsonPoint}</li>
67+ * <li>{@link GeoJsonMultiPoint}</li>
68+ * <li>{@link GeoJsonLineString}</li>
69+ * <li>{@link GeoJsonMultiLineString}</li>
70+ * <li>{@link GeoJsonPolygon}</li>
71+ * <li>{@link GeoJsonMultiPolygon}</li>
72+ * </ul>
73+ *
74+ * @return a {@link Module} containing {@link JsonDeserializer deserializers} for {@link GeoJson} types.
75+ * @since 3.2
76+ */
77+ public static Module deserializers () {
78+
79+ SimpleModule module = new SimpleModule ("Spring Data MongoDB GeoJson - Deserializers" ,
80+ new Version (3 , 2 , 0 , null , "org.springframework.data" , "spring-data-mongodb-geojson" ));
81+ registerDeserializersIn (module );
82+ return module ;
83+ }
84+
85+ /**
86+ * Obtain a {@link Module} containing {@link JsonSerializer serializers} for the following {@link GeoJson} types:
87+ * <ul>
88+ * <li>{@link GeoJsonPoint}</li>
89+ * <li>{@link GeoJsonMultiPoint}</li>
90+ * <li>{@link GeoJsonLineString}</li>
91+ * <li>{@link GeoJsonMultiLineString}</li>
92+ * <li>{@link GeoJsonPolygon}</li>
93+ * <li>{@link GeoJsonMultiPolygon}</li>
94+ * </ul>
95+ *
96+ * @return a {@link Module} containing {@link JsonSerializer serializers} for {@link GeoJson} types.
97+ * @since 3.2
98+ */
99+ public static Module serializers () {
100+
101+ SimpleModule module = new SimpleModule ("Spring Data MongoDB GeoJson - Serializers" ,
102+ new Version (3 , 2 , 0 , null , "org.springframework.data" , "spring-data-mongodb-geojson" ));
103+ registerSerializersIn (module );
104+ return module ;
105+ }
106+
107+ /**
108+ * Obtain a {@link Module} containing {@link JsonSerializer serializers} and {@link JsonDeserializer deserializers}
109+ * for the following {@link GeoJson} types:
110+ * <ul>
111+ * <li>{@link GeoJsonPoint}</li>
112+ * <li>{@link GeoJsonMultiPoint}</li>
113+ * <li>{@link GeoJsonLineString}</li>
114+ * <li>{@link GeoJsonMultiLineString}</li>
115+ * <li>{@link GeoJsonPolygon}</li>
116+ * <li>{@link GeoJsonMultiPolygon}</li>
117+ * </ul>
118+ *
119+ * @return a {@link Module} containing {@link JsonSerializer serializers} and {@link JsonDeserializer deserializers}
120+ * for {@link GeoJson} types.
121+ * @since 3.2
122+ */
123+ public static Module geoJsonModule () {
124+
125+ SimpleModule module = new SimpleModule ("Spring Data MongoDB GeoJson" ,
126+ new Version (3 , 2 , 0 , null , "org.springframework.data" , "spring-data-mongodb-geojson" ));
127+ registerSerializersIn (module );
128+ registerDeserializersIn (module );
129+ return module ;
130+ }
131+
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+
142+ private static void registerDeserializersIn (SimpleModule module ) {
143+
144+ module .addDeserializer (GeoJsonPoint .class , new GeoJsonPointDeserializer ());
145+ module .addDeserializer (GeoJsonMultiPoint .class , new GeoJsonMultiPointDeserializer ());
146+ module .addDeserializer (GeoJsonLineString .class , new GeoJsonLineStringDeserializer ());
147+ module .addDeserializer (GeoJsonMultiLineString .class , new GeoJsonMultiLineStringDeserializer ());
148+ module .addDeserializer (GeoJsonPolygon .class , new GeoJsonPolygonDeserializer ());
149+ module .addDeserializer (GeoJsonMultiPolygon .class , new GeoJsonMultiPolygonDeserializer ());
56150 }
57151
58152 /**
@@ -67,8 +161,7 @@ private static abstract class GeoJsonDeserializer<T extends GeoJson<?>> extends
67161 */
68162 @ Nullable
69163 @ Override
70- public T deserialize (@ Nullable JsonParser jp , @ Nullable DeserializationContext ctxt )
71- throws IOException , JsonProcessingException {
164+ public T deserialize (@ Nullable JsonParser jp , @ Nullable DeserializationContext ctxt ) throws IOException {
72165
73166 JsonNode node = jp .readValueAsTree ();
74167 JsonNode coordinates = node .get ("coordinates" );
0 commit comments