suerprisePlus
2024-09-04 85af9acfccf2e944a97557d3e46d143b0e99e2f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.yb.postgis;
 
 
import java.io.IOException;
 
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.common.base.Strings;
//import jdk.internal.joptsimple.internal.Strings;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
 
 
/**
 * Deserialize WKT property as Geometry Object with Jackson<br>
 *
 * @param <T>
 * @author guyadong
 */
public class GeometryDeserializer<T extends Geometry> extends JsonDeserializer<T> {
 
    @SuppressWarnings("unchecked")
    @Override
    public T deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException, JsonProcessingException {
        try {
            String text = jp.getValueAsString();
            if (Strings.isNullOrEmpty(text)) {
                return null;
            }
            WKTReader reader = new WKTReader();
            return (T) reader.read(text);
        } catch (ParseException e) {
            throw new IOException(e);
        }
    }
}