17
17
*/
18
18
package com .graphhopper .http ;
19
19
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 ;
20
30
import com .graphhopper .GHResponse ;
21
31
import com .graphhopper .PathWrapper ;
22
32
import com .graphhopper .util .Helper ;
25
35
import com .graphhopper .util .exceptions .GHException ;
26
36
import com .graphhopper .util .shapes .BBox ;
27
37
38
+ import java .text .NumberFormat ;
39
+ import java .text .SimpleDateFormat ;
28
40
import java .util .*;
41
+ import java .util .stream .Collectors ;
29
42
30
43
/**
31
44
* @author Peter Karich
32
45
*/
33
46
public class SimpleRouteSerializer implements RouteSerializer {
34
47
private final BBox maxBounds ;
48
+ private final ObjectMapper objectMapper = new ObjectMapper ();
35
49
36
50
public SimpleRouteSerializer (BBox maxBounds ) {
37
51
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
+
38
73
}
39
74
40
75
private String getMessage (Throwable t ) {
@@ -77,6 +112,7 @@ public Map<String, Object> toJSON(GHResponse rsp,
77
112
jsonPath .put ("distance" , Helper .round (ar .getDistance (), 3 ));
78
113
jsonPath .put ("weight" , Helper .round6 (ar .getRouteWeight ()));
79
114
jsonPath .put ("time" , ar .getTime ());
115
+ jsonPath .put ("transfers" , ar .getNumChanges ());
80
116
if (!ar .getDescription ().isEmpty ())
81
117
jsonPath .put ("description" , ar .getDescription ());
82
118
@@ -96,11 +132,16 @@ public Map<String, Object> toJSON(GHResponse rsp,
96
132
jsonPath .put ("instructions" , instructions .createJson ());
97
133
}
98
134
135
+ jsonPath .put ("legs" , objectMapper .convertValue (ar .getLegs (), new TypeReference <List <Map <String , Object >>>() {}));
136
+
99
137
jsonPath .put ("ascend" , ar .getAscend ());
100
138
jsonPath .put ("descend" , ar .getDescend ());
101
139
}
102
140
103
141
jsonPath .put ("snapped_waypoints" , createPoints (ar .getWaypoints (), pointsEncoded , includeElevation ));
142
+ if (ar .getFare () != null ) {
143
+ jsonPath .put ("fare" , NumberFormat .getCurrencyInstance ().format (ar .getFare ()));
144
+ }
104
145
jsonPathList .add (jsonPath );
105
146
}
106
147
0 commit comments