1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.bremersee.geojson.converter.deserialization;
18
19 import static java.util.Objects.isNull;
20
21 import java.util.Map;
22 import org.bremersee.geojson.GeoJsonGeometryFactory;
23 import org.locationtech.jts.geom.Geometry;
24 import org.locationtech.jts.geom.GeometryFactory;
25 import tools.jackson.core.JsonParser;
26 import tools.jackson.core.type.TypeReference;
27 import tools.jackson.databind.DeserializationContext;
28 import tools.jackson.databind.deser.std.StdDeserializer;
29
30
31
32
33
34
35 public class JacksonGeometryDeserializer extends StdDeserializer<Geometry> {
36
37
38
39
40 private final JsonToGeometryConverter geometryConverter;
41
42
43
44
45 public JacksonGeometryDeserializer() {
46 this(new GeometryFactory());
47 }
48
49
50
51
52
53
54 public JacksonGeometryDeserializer(GeometryFactory geometryFactory) {
55 super(Geometry.class);
56 GeometryFactory gf = isNull(geometryFactory) ? new GeoJsonGeometryFactory() : geometryFactory;
57 this.geometryConverter = new JsonToGeometryConverter(gf);
58 }
59
60 @Override
61 public Geometry deserialize(JsonParser jp, DeserializationContext ctxt) {
62 Map<String, Object> map = jp.readValueAs(new MapTypeReference());
63 if (isNull(map) || map.isEmpty()) {
64 return null;
65 }
66 return geometryConverter.convert(map);
67 }
68
69 private static class MapTypeReference extends TypeReference<Map<String, Object>> {
70
71 private MapTypeReference() {
72 }
73 }
74 }