Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent Jackson from unwrapping List in Geometry #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/org/geojson/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;

public abstract class Geometry<T> extends GeoJsonObject {

@JsonFormat(without = JsonFormat.Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
protected List<T> coordinates = new ArrayList<T>();

public Geometry() {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/geojson/jackson/MultiLineStringTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.geojson.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.geojson.LngLatAlt;
import org.geojson.MultiLineString;
import org.junit.Test;
Expand All @@ -21,4 +23,15 @@ public void itShouldSerialize() throws Exception {
assertEquals("{\"type\":\"MultiLineString\",\"coordinates\":"
+ "[[[100.0,0.0],[101.0,1.0]],[[102.0,2.0],[103.0,3.0]]]}", mapper.writeValueAsString(multiLineString));
}

@Test
public void itShouldSerializeIgnoringWriteSingleElemArraysUnwrapped() throws Exception {
ObjectMapper mapper = new ObjectMapper()
.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);

MultiLineString multiLineString = new MultiLineString();
multiLineString.add(Arrays.asList(new LngLatAlt(100, 0), new LngLatAlt(101, 1)));
assertEquals("{\"type\":\"MultiLineString\",\"coordinates\":"
+ "[[[100.0,0.0],[101.0,1.0]]]}", mapper.writeValueAsString(multiLineString));
}
}
20 changes: 20 additions & 0 deletions src/test/java/org/geojson/jackson/MultiPoligonTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.geojson.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.geojson.LngLatAlt;
import org.geojson.MultiPolygon;
import org.geojson.Polygon;
Expand All @@ -27,6 +29,24 @@ public void itShouldSerialize() throws Exception {
mapper.writeValueAsString(multiPolygon));
}

@Test
public void itShouldSerializeIgnoringWriteSingleElemArraysUnwrapped() throws Exception {
ObjectMapper mapper = new ObjectMapper()
.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);

MultiPolygon multiPolygon = new MultiPolygon();
multiPolygon.add(new Polygon(new LngLatAlt(102, 2), new LngLatAlt(103, 2), new LngLatAlt(103, 3),
new LngLatAlt(102, 3), new LngLatAlt(102, 2)));
Polygon polygon = new Polygon(MockData.EXTERNAL);
polygon.addInteriorRing(MockData.INTERNAL);
multiPolygon.add(polygon);
assertEquals(
"{\"type\":\"MultiPolygon\",\"coordinates\":[[[[102.0,2.0],[103.0,2.0],[103.0,3.0],[102.0,3.0],[102.0,2.0]]],"
+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]]}",
mapper.writeValueAsString(multiPolygon));
}

@Test
public void itShouldDeserialize() throws Exception {
MultiPolygon multiPolygon = mapper.readValue(
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/org/geojson/jackson/PolygonTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.geojson.jackson;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import org.geojson.LngLatAlt;
import org.geojson.Polygon;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand All @@ -23,6 +24,19 @@ public void itShouldSerialize() throws Exception {
mapper.writeValueAsString(polygon));
}

@Test
public void itShouldSerializeIgnoringWriteSingleElemArraysUnwrapped() throws Exception {
ObjectMapper mapper = new ObjectMapper()
.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED)
;

Polygon polygon = new Polygon(MockData.EXTERNAL);
String actual = mapper.writeValueAsString(polygon);
assertEquals("{\"type\":\"Polygon\",\"coordinates\":"
+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}",
actual);
}

@Test
public void itShouldSerializeWithHole() throws Exception {
Polygon polygon = new Polygon(MockData.EXTERNAL);
Expand Down