-
Notifications
You must be signed in to change notification settings - Fork 171
Closed
Description
The ShapesAsWKTModule serializes Geometry objects just fine, but it cannot deserialize them. The object returned is null.
Here is a test to demonstrate the issue:
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.junit.Test;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.spatial4j.io.jackson.ShapesAsWKTModule;
public class ObjectMapperConfigurationTest {
public static class GeomWrapper {
private Geometry geometry;
public Geometry getGeometry() {
return geometry;
}
public void setGeometry(Geometry geometry) {
this.geometry = geometry;
}
}
private static Geometry createGeometry(String wkt) throws ParseException {
GeometryFactory geometryFactory = new GeometryFactory(new PrecisionModel(), 4326);
WKTReader wktReader = new WKTReader(geometryFactory);
return wktReader.read(wkt);
}
@Test
public void test() throws Exception {
final String wkt = "POINT (30 10)";
final Geometry geometry = createGeometry(wkt);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.registerModule(new ShapesAsWKTModule());
objectMapper.registerModule(new JavaTimeModule());
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
final GeomWrapper toSerialize = new GeomWrapper();
toSerialize.setGeometry(geometry);
String json = objectMapper.writeValueAsString(toSerialize);
System.out.println(json);
assertEquals("{\"geometry\":\"POINT (30 10)\"}", json);
final GeomWrapper deserialized = objectMapper.readValue(json, GeomWrapper.class);
assertNotNull(deserialized.getGeometry());
}
}```
Metadata
Metadata
Assignees
Labels
No labels