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

相关内容

热门资讯

韩国计划将可再生能源发电占比提... 每经AI快讯,韩国气候能源环境部6日宣布,韩国计划到2030年将可再生能源发电占比提升到至少20%,...
河北护航民营经济高质量发展(“... 来源:人民日报深化制度改革 提升要素保障 强化政企沟通河北护航民营经济高质量发展(“十五五”开好局起...
亚航长程票价最高上涨40% 首...   马来西亚廉价航空公司亚航长程将机票价格上调最高40%,并将燃油附加费提高了20%,此前伊朗战争爆...
谁是顺义最圈粉的鸟?“飞羽之星... 春风渐暖,飞羽归来,最近顺义开始热闹了起来——潮白河上,天鹅优雅“营业”;汉石桥湿地里,芦苇轻摇,鸟...
美军营救行动更多细节曝光 ... 转自:新华国际头条美国空军一架F-15E型“打击鹰”双座战斗机3日在伊朗伊斯法罕省上空被击落,机上两...