Closed
Description
Bjorn Harvold opened DATAMONGO-2664 and commented
I'm trying to attach screenshots but getting "Try again. Invalid token". Will try to attach after I create the ticket.
The existing test that accompanies Spring Data MongoDb is in the class called GeoJsonModuleUnitTests:
@Test // DATAMONGO-1181 public void shouldDeserializeGeoJsonPolygonCorrectly() throws JsonParseException, JsonMappingException, IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(new GeoJsonPolygon( Arrays.asList(new Point(100, 0), new Point(101, 0), new Point(101, 1), new Point(100, 1), new Point(100, 0)))); }
And that passes just fine.
However, change those points to GeoJsonPoint objects and it fails:
@Test
public void shouldSerializeGeoJsonPolygonCorrectly() throws IOException {
String json = "{ \"type\": \"Polygon\", \"coordinates\": [ [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] ]}";
List<Point> points = Arrays.asList(new GeoJsonPoint(100, 0), new GeoJsonPoint(101, 0), new GeoJsonPoint(101, 1), new GeoJsonPoint(100, 1), new GeoJsonPoint(100, 0));
GeoJsonPolygon polygon = new GeoJsonPolygon(points);
assertThat(mapper.readValue(json, GeoJsonPolygon.class)).isEqualTo(polygon);
assertThat(mapper.writeValueAsString(polygon)).isEqualTo(json);
}
And that is exactly what happens when I query MongoDb (simple findById()) for a document with Spring Data MongoDb. When I look in the object graph, I see GeoJsonPoint objects as coordinates in my Polygon.
When the Jackson ObjectMapper serializes that data, it looks like this:
"polygon" : {
"points" : [ {
"x" : 105.44848312743373,
"y" : 13.049959719225617,
"type" : "Point",
"coordinates" : [ 105.44848312743373, 13.049959719225617 ]
}, {
"x" : 105.11889328368373,
"y" : 11.891452140925736,
"type" : "Point",
"coordinates" : [ 105.11889328368373, 11.891452140925736 ]
}, {
"x" : 105.69018234618373,
"y" : 11.50415959858532,
"type" : "Point",
"coordinates" : [ 105.69018234618373, 11.50415959858532 ]
}, {
"x" : 105.88793625243373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 105.88793625243373, 11.998936428709188 ]
}, {
"x" : 106.81078781493373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 106.81078781493373, 11.998936428709188 ]
}, {
"x" : 107.29418625243373,
"y" : 12.600046596262514,
"type" : "Point",
"coordinates" : [ 107.29418625243373, 12.600046596262514 ]
}, {
"x" : 107.22826828368373,
"y" : 13.584502655549848,
"type" : "Point",
"coordinates" : [ 107.22826828368373, 13.584502655549848 ]
}, {
"x" : 106.48119797118373,
"y" : 12.964324199217307,
"type" : "Point",
"coordinates" : [ 106.48119797118373, 12.964324199217307 ]
}, {
"x" : 106.41528000243373,
"y" : 14.543620849640376,
"type" : "Point",
"coordinates" : [ 106.41528000243373, 14.543620849640376 ]
} ],
"coordinates" : [ {
"type" : "LineString",
"coordinates" : [ {
"x" : 105.44848312743373,
"y" : 13.049959719225617,
"type" : "Point",
"coordinates" : [ 105.44848312743373, 13.049959719225617 ]
}, {
"x" : 105.11889328368373,
"y" : 11.891452140925736,
"type" : "Point",
"coordinates" : [ 105.11889328368373, 11.891452140925736 ]
}, {
"x" : 105.69018234618373,
"y" : 11.50415959858532,
"type" : "Point",
"coordinates" : [ 105.69018234618373, 11.50415959858532 ]
}, {
"x" : 105.88793625243373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 105.88793625243373, 11.998936428709188 ]
}, {
"x" : 106.81078781493373,
"y" : 11.998936428709188,
"type" : "Point",
"coordinates" : [ 106.81078781493373, 11.998936428709188 ]
}, {
"x" : 107.29418625243373,
"y" : 12.600046596262514,
"type" : "Point",
"coordinates" : [ 107.29418625243373, 12.600046596262514 ]
}, {
"x" : 107.22826828368373,
"y" : 13.584502655549848,
"type" : "Point",
"coordinates" : [ 107.22826828368373, 13.584502655549848 ]
}, {
"x" : 106.48119797118373,
"y" : 12.964324199217307,
"type" : "Point",
"coordinates" : [ 106.48119797118373, 12.964324199217307 ]
}, {
"x" : 106.41528000243373,
"y" : 14.543620849640376,
"type" : "Point",
"coordinates" : [ 106.41528000243373, 14.543620849640376 ]
} ]
} ],
"type" : "Polygon"
}
Please advise,
Bjorn
Affects: 3.1.1 (2020.0.1)
Attachments:
- GeoJsonPolygon_Document.png (343.80 kB)
- GeoJsonPolygon_Object.png (189.50 kB)