1 /*
2  * Copyright 2014-2020 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.geo;
17
18 import java.util.List;
19
20 import com.fasterxml.jackson.annotation.JsonIgnore;
21 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
22 import com.fasterxml.jackson.annotation.JsonProperty;
23 import com.fasterxml.jackson.core.Version;
24 import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
25 import com.fasterxml.jackson.databind.module.SimpleModule;
26
27 /**
28  * Custom module to deserialize the geo-spatial value objects using Jackson 2.
29  *
30  * @author Oliver Gierke
31  * @since 1.8
32  */

33 public class GeoModule extends SimpleModule {
34
35     private static final long serialVersionUID = 1L;
36
37     /**
38      * Creates a new {@link GeoModule} registering mixins for common geo-spatial types.
39      */

40     public GeoModule() {
41
42         super("Spring Data Geo Mixins"new Version(1, 0, 0, null"org.springframework.data""spring-data-commons-geo"));
43
44         setMixInAnnotation(Distance.class, DistanceMixin.class);
45         setMixInAnnotation(Point.class, PointMixin.class);
46         setMixInAnnotation(Box.class, BoxMixin.class);
47         setMixInAnnotation(Circle.class, CircleMixin.class);
48         setMixInAnnotation(Polygon.class, PolygonMixin.class);
49     }
50
51     @JsonIgnoreProperties("unit")
52     static abstract class DistanceMixin {
53
54         DistanceMixin(@JsonProperty("value"double value,
55                 @JsonProperty("metric") @JsonDeserialize(as = Metrics.class) Metric metic) {}
56
57         @JsonIgnore
58         abstract double getNormalizedValue();
59     }
60
61     static abstract class PointMixin {
62         PointMixin(@JsonProperty("x"double x, @JsonProperty("y"double y) {}
63     }
64
65     static abstract class CircleMixin {
66         CircleMixin(@JsonProperty("center") Point center, @JsonProperty("radius") Distance radius) {}
67     }
68
69     static abstract class BoxMixin {
70         BoxMixin(@JsonProperty("first") Point first, @JsonProperty("second") Point point) {}
71     }
72
73     static abstract class PolygonMixin {
74         PolygonMixin(@JsonProperty("points") List<Point> points) {}
75     }
76 }
77