62 lines
2.2 KiB
Java
62 lines
2.2 KiB
Java
|
|
package com.cdzy.user.handler;
|
||
|
|
|
||
|
|
import com.fasterxml.jackson.core.JsonParser;
|
||
|
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||
|
|
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
||
|
|
import org.postgresql.geometric.PGpoint;
|
||
|
|
|
||
|
|
import java.io.IOException;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* PGpoint 反序列化器 - 将 JSON 直接反序列化为 PGpoint
|
||
|
|
*/
|
||
|
|
public class PGpointDeserializer extends JsonDeserializer<PGpoint> {
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public PGpoint deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||
|
|
JsonNode node = p.getCodec().readTree(p);
|
||
|
|
|
||
|
|
if (node.isNull()) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
|
try {
|
||
|
|
// 方式1: GeoJSON 格式
|
||
|
|
if (node.isObject() && node.has("type") && "point".equals(node.get("type").asText())) {
|
||
|
|
JsonNode longitude = node.get("longitude");
|
||
|
|
JsonNode latitude = node.get("latitude");
|
||
|
|
if (longitude != null && latitude != null) {
|
||
|
|
double x = longitude.asDouble();
|
||
|
|
double y =latitude.asDouble();
|
||
|
|
return new PGpoint(x, y);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// 方式2: 简单对象格式 { "x": 116.3974, "y": 39.9093 }
|
||
|
|
else if (node.isObject() && node.has("x") && node.has("y")) {
|
||
|
|
double x = node.get("x").asDouble();
|
||
|
|
double y = node.get("y").asDouble();
|
||
|
|
return new PGpoint(x, y);
|
||
|
|
}
|
||
|
|
// 方式3: 数组格式 [116.3974, 39.9093]
|
||
|
|
else if (node.isArray() && node.size() >= 2) {
|
||
|
|
double x = node.get(0).asDouble();
|
||
|
|
double y = node.get(1).asDouble();
|
||
|
|
return new PGpoint(x, y);
|
||
|
|
}
|
||
|
|
// 方式4: 字符串格式 "(116.3974,39.9093)"
|
||
|
|
else if (node.isTextual()) {
|
||
|
|
return new PGpoint(node.asText());
|
||
|
|
}
|
||
|
|
} catch (Exception e) {
|
||
|
|
throw new IOException("Failed to deserialize PGpoint: " + node, e);
|
||
|
|
}
|
||
|
|
|
||
|
|
throw new IOException("Unsupported PGpoint format: " + node);
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public Class<PGpoint> handledType() {
|
||
|
|
return PGpoint.class;
|
||
|
|
}
|
||
|
|
}
|