Skip to content

ShapesAsWKTModule does not deserialize WKT, returns null #175

@electricsam

Description

@electricsam

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions