geometry:Jackson实现对JTS geometry对象的序列化和反序列化
创始人
2025-05-29 13:30:58

在上一篇博文《geometry:MySQL的空间数据类型(Spatial Data Type)与JTS(OSGeo)类型之间的序列化和反序列化》中,实现了对MySQL数据库存储的WKB数据到JTS Geometry对象之间的转换。
当我们从数据库中得到的Geometry对象后,我们需要把它提供给前端时,就需要将它转为JSON格式,或从前端将JSON数据反序列化为Geometry对象。本文说明使用JSON工具库Jackson如何实现这个过程。
JTS Geometry对象不是标准的Java Bean不能自动被Jackson执行序列化和反序列化。所以我们需要为 Geometry对象实现自定义的序列化器和反序列化器。
我的实现方式就是将Geometry对象序列化为字符串,即WKT(Well-Known Text)字符串。

JTS库依赖引入

		com.vividsolutionsjts1.13

序列化器实现


import java.io.IOException;import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.vividsolutions.jts.geom.Geometry;/*** Serializing Geometry property as WKT String with Jackson
* @author guyadong* @param */ public class GeometrySerializer extends JsonSerializer {@Overridepublic void serialize(T value, JsonGenerator gen, SerializerProvider serializers)throws IOException, JsonProcessingException {gen.writeString(value.toText());} }

反序列化器实现


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 com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;/*** Deserialize WKT property as Geometry Object with Jackson
* @author guyadong* @param */ public class GeometryDeserializer extends JsonDeserializer {@SuppressWarnings("unchecked")@Overridepublic 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);}} }

注解方式引用

如下可以在以使用JsonDeserialize和JsonSerialize注解定义类成员字段的自定义序列化和反序列化器:

    @com.fasterxml.jackson.databind.annotation.JsonDeserialize(using = gu.sql2java.geometry.jackson.GeometryDeserializer.class)@com.fasterxml.jackson.databind.annotation.JsonSerialize(using = gu.sql2java.geometry.jackson.GeometrySerializer.class)private com.vividsolutions.jts.geom.Point spot;

调用示例

import static org.junit.Assert.*;import org.junit.FixMethodOrder;import org.junit.Test;
import org.junit.runners.MethodSorters;import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.vividsolutions.jts.geom.Point;public class GeometryJacksonTest {@Testpublic void test1Codec() {try {/** JSON 格式字符串,保存WKT格式的坐标数据 */String jsonWKT = "\"POINT (1.75 -1.222)\"";System.out.printf("jsonWKT  \t%s\n",jsonWKT);ObjectMapper objectMapper = new ObjectMapper();SimpleModule simpleModule = new SimpleModule();/** 指定 Point类的序列化器 */simpleModule.addSerializer(Point.class, new GeometrySerializer());/** 指定 Point类的反序列化器 */simpleModule.addDeserializer(Point.class, new GeometryDeserializer());objectMapper.registerModule(simpleModule);/** 反序列化为Point对象 */Point deserialized = objectMapper.readValue(jsonWKT,Point.class);System.out.printf("deserialized\t%s\n",deserialized.toText());/** 序列化为JSON 字符串 */String serialized = objectMapper.writeValueAsString(deserialized);System.out.printf("serialized  \t%s\n",serialized);assertTrue(jsonWKT.equals(serialized));} catch (Exception e) {e.printStackTrace();assertTrue(false);}}
}

输出

jsonWKT “POINT (1.75 -1.222)”
deserialized POINT (1.75 -1.222)
serialized “POINT (1.75 -1.222)”

完整代码参见我的码云仓库:https://gitee.com/l0km/sql2java/tree/dev/sql2java-base/src/main/java/gu/sql2java/geometry/jackson
https://gitee.com/l0km/sql2java/blob/dev/sql2java-base/src/test/java/gu/sql2java/GeometryJacksonTest.java

相关内容

热门资讯

涉嫌信披违规 大烨智能被立案... 近日,大烨智能发布关于收到中国证券监督管理委员会立案告知书的公告。从中可以看到:公司于2025 年1...
1至11月新设“8+9”相关产... 本报北京12月26日电(记者鲁元珍)市场监管总局26日发布的最新数据显示,今年以来,我国“8+9”相...
移... 前两天和一个同事聊天。 他表示,对于经常出差、需要每天写文章的人来说,确实需要一个云服务软件。 事实...
L... ◆概述病毒和恶意软件日益成为计算机系统的最大威胁。 近年来,随着Linux系统在云计算和企业服务中的...
火... 目录1、软件介绍2.下载地址软件介绍Tinder 安全软件在用户中拥有非常好的声誉。 它完全免费,没...