Skip to content

Commit 32e9beb

Browse files
committed
RouteSerializer can now serialize Legs.
1 parent eb29b73 commit 32e9beb

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

web/src/main/java/com/graphhopper/http/SimpleRouteSerializer.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
*/
1818
package com.graphhopper.http;
1919

20+
import com.bedatadriven.jackson.datatype.jts.JtsModule;
21+
import com.fasterxml.jackson.core.JsonGenerator;
22+
import com.fasterxml.jackson.core.type.TypeReference;
23+
import com.fasterxml.jackson.databind.BeanDescription;
24+
import com.fasterxml.jackson.databind.ObjectMapper;
25+
import com.fasterxml.jackson.databind.SerializationConfig;
26+
import com.fasterxml.jackson.databind.SerializerProvider;
27+
import com.fasterxml.jackson.databind.module.SimpleModule;
28+
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
29+
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
2030
import com.graphhopper.GHResponse;
2131
import com.graphhopper.PathWrapper;
2232
import com.graphhopper.util.Helper;
@@ -25,16 +35,41 @@
2535
import com.graphhopper.util.exceptions.GHException;
2636
import com.graphhopper.util.shapes.BBox;
2737

38+
import java.text.NumberFormat;
39+
import java.text.SimpleDateFormat;
2840
import java.util.*;
41+
import java.util.stream.Collectors;
2942

3043
/**
3144
* @author Peter Karich
3245
*/
3346
public class SimpleRouteSerializer implements RouteSerializer {
3447
private final BBox maxBounds;
48+
private final ObjectMapper objectMapper = new ObjectMapper();
3549

3650
public SimpleRouteSerializer(BBox maxBounds) {
3751
this.maxBounds = maxBounds;
52+
this.objectMapper.setDateFormat(new SimpleDateFormat("YYYY-MM-dd'T'HH:mm")); // ISO8601 without time zone
53+
this.objectMapper.registerModule(new JtsModule());
54+
55+
// Because VirtualEdgeIteratorState has getters which throw Exceptions.
56+
// http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
57+
this.objectMapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {
58+
@Override
59+
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
60+
return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {
61+
@Override
62+
public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
63+
try {
64+
super.serializeAsField(bean, gen, prov);
65+
} catch (Exception e) {
66+
// Ignoring expected exception, see above.
67+
}
68+
}
69+
}).collect(Collectors.toList());
70+
}
71+
}));
72+
3873
}
3974

4075
private String getMessage(Throwable t) {
@@ -77,6 +112,7 @@ public Map<String, Object> toJSON(GHResponse rsp,
77112
jsonPath.put("distance", Helper.round(ar.getDistance(), 3));
78113
jsonPath.put("weight", Helper.round6(ar.getRouteWeight()));
79114
jsonPath.put("time", ar.getTime());
115+
jsonPath.put("transfers", ar.getNumChanges());
80116
if (!ar.getDescription().isEmpty())
81117
jsonPath.put("description", ar.getDescription());
82118

@@ -96,11 +132,16 @@ public Map<String, Object> toJSON(GHResponse rsp,
96132
jsonPath.put("instructions", instructions.createJson());
97133
}
98134

135+
jsonPath.put("legs", objectMapper.convertValue(ar.getLegs(), new TypeReference<List<Map<String, Object>>>() {}));
136+
99137
jsonPath.put("ascend", ar.getAscend());
100138
jsonPath.put("descend", ar.getDescend());
101139
}
102140

103141
jsonPath.put("snapped_waypoints", createPoints(ar.getWaypoints(), pointsEncoded, includeElevation));
142+
if (ar.getFare() != null) {
143+
jsonPath.put("fare", NumberFormat.getCurrencyInstance().format(ar.getFare()));
144+
}
104145
jsonPathList.add(jsonPath);
105146
}
106147

0 commit comments

Comments
 (0)