Skip to content

Commit

Permalink
avoid serializing empty lists
Browse files Browse the repository at this point in the history
  • Loading branch information
karussell committed Nov 26, 2019
1 parent fc1a4e6 commit 847ea68
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,29 +89,24 @@ public MatrixResponse route(GHMRequest ghRequest) {
outArraysList.add("weights");
}

ArrayNode outArrayListJson = objectMapper.createArrayNode();
for (String str : outArraysList) {
outArrayListJson.add(str);
}

boolean hasElevation = false;
if (ghRequest.identicalLists) {
requestJson.putArray("points").addAll(createPointList(ghRequest.getFromPoints()));
requestJson.putArray("point_hints").addAll(createStringList(ghRequest.getFromPointHints()));
requestJson.putArray("curbsides").addAll(createStringList(ghRequest.getFromCurbsides()));
createPointArray(requestJson, "points", ghRequest.getFromPoints());
createStringArray(requestJson, "point_hints", ghRequest.getFromPointHints());
createStringArray(requestJson, "curbsides", ghRequest.getFromCurbsides());
} else {
ArrayNode fromPointList = createPointList(ghRequest.getFromPoints());
ArrayNode toPointList = createPointList(ghRequest.getToPoints());
requestJson.putArray("from_points").addAll(fromPointList);
requestJson.putArray("from_point_hints").addAll(createStringList(ghRequest.getFromPointHints()));
requestJson.putArray("to_points").addAll(toPointList);
requestJson.putArray("to_point_hints").addAll(createStringList(ghRequest.getToPointHints()));
requestJson.putArray("from_curbsides").addAll(createStringList(ghRequest.getFromCurbsides()));
requestJson.putArray("to_curbsides").addAll(createStringList(ghRequest.getToPointHints()));
createPointArray(requestJson, "from_points", ghRequest.getFromPoints());
createStringArray(requestJson, "from_point_hints", ghRequest.getFromPointHints());

createPointArray(requestJson, "to_points", ghRequest.getToPoints());
createStringArray(requestJson, "to_point_hints", ghRequest.getToPointHints());

createStringArray(requestJson, "from_curbsides", ghRequest.getFromCurbsides());
createStringArray(requestJson, "to_curbsides", ghRequest.getToCurbsides());
}

requestJson.putArray("snap_preventions").addAll(createStringList(ghRequest.getSnapPreventions()));
requestJson.putArray("out_arrays").addAll(outArrayListJson);
createStringArray(requestJson, "snap_preventions", ghRequest.getSnapPreventions());
createStringArray(requestJson, "out_arrays", outArraysList);
requestJson.put("vehicle", ghRequest.getVehicle());
requestJson.put("elevation", hasElevation);
requestJson.put("fail_fast", ghRequest.getFailFast());
Expand Down Expand Up @@ -222,22 +217,26 @@ protected String postJson(String url, JsonNode data) throws IOException {
}
}

private ArrayNode createStringList(List<String> list) {
private void createStringArray(ObjectNode requestJson, String name, List<String> stringList) {
if (stringList.isEmpty())
return;
ArrayNode outList = objectMapper.createArrayNode();
for (String str : list) {
for (String str : stringList) {
outList.add(str);
}
return outList;
requestJson.putArray(name).addAll(outList);
}

private ArrayNode createPointList(List<GHPoint> list) {
private void createPointArray(ObjectNode requestJson, String name, List<GHPoint> pList) {
if (pList.isEmpty())
return;
ArrayNode outList = objectMapper.createArrayNode();
for (GHPoint p : list) {
for (GHPoint p : pList) {
ArrayNode entry = objectMapper.createArrayNode();
entry.add(p.lon);
entry.add(p.lat);
outList.add(entry);
}
return outList;
requestJson.putArray(name).addAll(outList);
}
}
23 changes: 23 additions & 0 deletions client-hc/src/test/java/com/graphhopper/api/GraphHopperWebIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -341,6 +342,28 @@ protected String postJson(String url, JsonNode data) throws IOException {
assertEquals("xy", req.getVehicle());
}

@Test
public void doNotIncludeEmptyCurbsidesList() {
final AtomicInteger counter = new AtomicInteger(0);
final GraphHopperMatrixWeb ghMatrix = new GraphHopperMatrixWeb(new GHMatrixBatchRequester() {
@Override
protected String postJson(String url, JsonNode data) throws IOException {
assertFalse(data.has("curbsides"));
assertTrue(data.has("points"));
counter.incrementAndGet();
return "";
}
});
GHMRequest req = new GHMRequest();
req.addPoint(new GHPoint(49.6724, 11.3494));
req.addPoint(new GHPoint(49.6550, 11.4180));
try {
ghMatrix.route(req);
} catch (Exception ex) {
}
assertEquals(1, counter.get());
}

@Test
public void testUnknownInstructionSign() throws IOException {
// Actual path for the request: point=48.354413%2C8.676335&point=48.35442%2C8.676345
Expand Down

0 comments on commit 847ea68

Please sign in to comment.