|
8 | 8 |
|
9 | 9 | package org.locationtech.spatial4j.io.jackson; |
10 | 10 |
|
| 11 | +import com.fasterxml.jackson.annotation.JsonInclude; |
11 | 12 | import com.fasterxml.jackson.databind.ObjectMapper; |
12 | 13 | import com.fasterxml.jackson.databind.SerializationFeature; |
13 | 14 | import org.junit.Test; |
| 15 | +import org.locationtech.jts.geom.Coordinate; |
14 | 16 | import org.locationtech.spatial4j.context.jts.JtsSpatialContext; |
15 | 17 | import org.locationtech.spatial4j.shape.RandomizedShapeTest; |
16 | | -import org.locationtech.spatial4j.util.GeomBuilder; |
| 18 | +import org.locationtech.spatial4j.shape.jts.JtsShapeFactory; |
17 | 19 |
|
18 | 20 | import java.io.IOException; |
19 | 21 |
|
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | + |
20 | 24 | public class SimpleJacksonTest extends RandomizedShapeTest { |
21 | 25 |
|
22 | 26 | public SimpleJacksonTest() { |
23 | 27 | super(JtsSpatialContext.GEO); |
24 | 28 | } |
25 | | - |
26 | | - @Test |
27 | | - public void testReadWrite() throws IOException { |
28 | 29 |
|
29 | | - GeomBuilder builder = new GeomBuilder(); |
30 | | - |
| 30 | + @Test |
| 31 | + public void testReadWriteShapeAsGeoJSON() throws IOException { |
31 | 32 | ObjectWithGeometry obj = new ObjectWithGeometry(); |
32 | 33 | obj.name = "Hello"; |
33 | | - obj.shape = randomPointIn(ctx.getWorldBounds()); |
| 34 | + obj.shape = ctx.getShapeFactory().pointXY(11,12); // Spatial4j type |
34 | 35 | obj.geo = null; // |
35 | | - |
| 36 | + |
36 | 37 | ObjectMapper mapper = new ObjectMapper(); |
37 | 38 | mapper.enable(SerializationFeature.INDENT_OUTPUT); |
38 | 39 | mapper.registerModule(new ShapesAsGeoJSONModule()); |
39 | | -// mapper.registerModule(new ShapesAsWKTModule()); |
40 | | - |
| 40 | + |
41 | 41 | String json = mapper.writeValueAsString(obj); |
42 | | - |
43 | | - System.out.println( json ); |
44 | | - |
| 42 | + |
45 | 43 | 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); |
46 | 63 |
|
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); |
49 | 66 | } |
50 | 67 | } |
0 commit comments