1 /*
2 * Copyright 2015-2022 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 * http://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
17 package org.bremersee.geojson.converter.serialization;
18
19 import org.locationtech.jts.geom.Geometry;
20 import tools.jackson.core.JsonGenerator;
21 import tools.jackson.databind.SerializationContext;
22 import tools.jackson.databind.ser.std.StdSerializer;
23
24 /**
25 * A Jackson serializer for a {@link Geometry}.
26 *
27 * @author Christian Bremer
28 */
29 public class JacksonGeometrySerializer extends StdSerializer<Geometry> {
30
31 /**
32 * The geometry to json converter.
33 */
34 private final GeometryToJsonConverter converter;
35
36 /**
37 * Instantiates a new Jackson geometry serializer.
38 */
39 public JacksonGeometrySerializer() {
40 this(false, false);
41 }
42
43 /**
44 * Instantiates a new Jackson geometry serializer.
45 *
46 * @param withBoundingBox the with bounding box
47 * @param useBigDecimal the use big decimal
48 */
49 public JacksonGeometrySerializer(boolean withBoundingBox, boolean useBigDecimal) {
50 super(Geometry.class);
51 this.converter = new GeometryToJsonConverter(withBoundingBox, useBigDecimal);
52 }
53
54 @Override
55 public void serialize(
56 Geometry value,
57 JsonGenerator jgen,
58 SerializationContext serializationContext) {
59
60 if (value == null) {
61 jgen.writeNull();
62 } else {
63 jgen.writePOJO(converter.convert(value));
64 }
65 }
66
67 }