Skip to content

Commit 63b3183

Browse files
committed
Jackson JSON deserialization didn't support WKT
Closes #175
1 parent 6a43828 commit 63b3183

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

CHANGES.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## VERSION 0.8
22

3-
DATE: unreleased
3+
DATE: _unreleased_
44

55
* \#177: Improve conversion of a Circle to Shape. JtsShapeFactory allows converting from a Shape
66
object to a JTS Geometry object. Geodetic circles now translate to a polygon that has points
@@ -13,6 +13,17 @@ DATE: unreleased
1313
* \#162: Fixed WKT & GeoJSON \[de\]serialization of "empty" points and geometrycollections.
1414
(Jeen Broekstra, David Smiley)
1515

16+
* \#165: Added ShapeFactory.pointLatLon convenience method.
17+
(MoeweX)
18+
19+
* \#167: WKTWriter now has a means to customize the NumberFromat.
20+
(MoeweX)
21+
22+
* \#175: ShapesAsWKTModule, a Jackson databind module,
23+
didn't deserialize WKT inside JSON to a Spatial4j Shape at all.
24+
Now it does. It continues to serialize correctly.
25+
(David Smiley)
26+
1627
## VERSION 0.7
1728

1829
DATE: 27 December 2017

src/main/java/org/locationtech/spatial4j/io/jackson/ShapeDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public Shape deserialize(JsonParser jp, DeserializationContext ctxt)
220220
return read( jp, ctx.getShapeFactory() );
221221
}
222222
if (t.isScalarValue()) {
223-
String txt = t.asString();
223+
String txt = jp.getValueAsString();
224224
if(txt!=null && txt.length()>0) {
225225
try {
226226
return ctx.getFormats().read(txt);

src/test/java/org/locationtech/spatial4j/io/jackson/SimpleJacksonTest.java

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,60 @@
88

99
package org.locationtech.spatial4j.io.jackson;
1010

11+
import com.fasterxml.jackson.annotation.JsonInclude;
1112
import com.fasterxml.jackson.databind.ObjectMapper;
1213
import com.fasterxml.jackson.databind.SerializationFeature;
1314
import org.junit.Test;
15+
import org.locationtech.jts.geom.Coordinate;
1416
import org.locationtech.spatial4j.context.jts.JtsSpatialContext;
1517
import org.locationtech.spatial4j.shape.RandomizedShapeTest;
16-
import org.locationtech.spatial4j.util.GeomBuilder;
18+
import org.locationtech.spatial4j.shape.jts.JtsShapeFactory;
1719

1820
import java.io.IOException;
1921

22+
import static org.junit.Assert.assertEquals;
23+
2024
public class SimpleJacksonTest extends RandomizedShapeTest {
2125

2226
public SimpleJacksonTest() {
2327
super(JtsSpatialContext.GEO);
2428
}
25-
26-
@Test
27-
public void testReadWrite() throws IOException {
2829

29-
GeomBuilder builder = new GeomBuilder();
30-
30+
@Test
31+
public void testReadWriteShapeAsGeoJSON() throws IOException {
3132
ObjectWithGeometry obj = new ObjectWithGeometry();
3233
obj.name = "Hello";
33-
obj.shape = randomPointIn(ctx.getWorldBounds());
34+
obj.shape = ctx.getShapeFactory().pointXY(11,12); // Spatial4j type
3435
obj.geo = null; //
35-
36+
3637
ObjectMapper mapper = new ObjectMapper();
3738
mapper.enable(SerializationFeature.INDENT_OUTPUT);
3839
mapper.registerModule(new ShapesAsGeoJSONModule());
39-
// mapper.registerModule(new ShapesAsWKTModule());
40-
40+
4141
String json = mapper.writeValueAsString(obj);
42-
43-
System.out.println( json );
44-
42+
4543
ObjectWithGeometry out = mapper.readValue(json, ObjectWithGeometry.class);
44+
assertEquals(obj.shape, out.shape);
45+
}
46+
47+
@Test
48+
public void testReadWriteJtsAsWKT() throws IOException {
49+
final JtsShapeFactory shapeFactory = ((JtsSpatialContext) ctx).getShapeFactory();
50+
51+
ObjectWithGeometry obj = new ObjectWithGeometry();
52+
obj.name = "Hello";
53+
obj.shape = null;
54+
obj.geo = shapeFactory.getGeometryFactory().createPoint(new Coordinate(11, 12)); // JTS type
55+
56+
ObjectMapper objectMapper = new ObjectMapper();
57+
objectMapper.registerModule(new ShapesAsWKTModule());
58+
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
59+
60+
String json = objectMapper.writeValueAsString(obj);
61+
62+
assertEquals("{\"name\":\"Hello\",\"geo\":\"POINT (11 12)\"}", json);
4663

47-
System.out.println( ">> AFTER <<" );
48-
System.out.println( mapper.writeValueAsString(out) );
64+
ObjectWithGeometry deserialized = objectMapper.readValue(json, ObjectWithGeometry.class);
65+
assertEquals(obj.geo, deserialized.geo);
4966
}
5067
}

0 commit comments

Comments
 (0)