File tree Expand file tree Collapse file tree
main/java/com/mapbox/api/directions/v5/models
test/java/com/mapbox/api/directions/v5/models Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import com .google .gson .GsonBuilder ;
44import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
5+ import com .mapbox .geojson .Point ;
6+ import com .mapbox .geojson .gson .PointSerializer ;
57
68import java .io .Serializable ;
79
@@ -22,6 +24,7 @@ public class DirectionsJsonObject implements Serializable {
2224 public String toJson () {
2325 GsonBuilder gson = new GsonBuilder ();
2426 gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
27+ gson .registerTypeAdapter (Point .class , new PointSerializer ());
2528 return gson .create ().toJson (this );
2629 }
2730}
Original file line number Diff line number Diff line change 77import com .google .gson .GsonBuilder ;
88import com .google .gson .TypeAdapter ;
99import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
10+ import com .mapbox .geojson .Point ;
11+ import com .mapbox .geojson .gson .PointDeserializer ;
1012
1113import java .util .List ;
1214
@@ -126,6 +128,7 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
126128 public static DirectionsResponse fromJson (String json ) {
127129 GsonBuilder gson = new GsonBuilder ();
128130 gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
131+ gson .registerTypeAdapter (Point .class , new PointDeserializer ());
129132 return gson .create ().fromJson (json , DirectionsResponse .class );
130133 }
131134
Original file line number Diff line number Diff line change 99import com .google .gson .annotations .SerializedName ;
1010import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
1111import com .mapbox .api .directions .v5 .MapboxDirections ;
12+ import com .mapbox .geojson .Point ;
13+ import com .mapbox .geojson .gson .PointDeserializer ;
1214
1315import java .util .List ;
1416
@@ -144,6 +146,7 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
144146 public static DirectionsRoute fromJson (String json ) {
145147 GsonBuilder gson = new GsonBuilder ();
146148 gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
149+ gson .registerTypeAdapter (Point .class , new PointDeserializer ());
147150 return gson .create ().fromJson (json , DirectionsRoute .class );
148151 }
149152
Original file line number Diff line number Diff line change 1111import com .mapbox .api .directions .v5 .DirectionsCriteria ;
1212import com .mapbox .api .directions .v5 .MapboxDirections ;
1313import com .mapbox .geojson .Point ;
14+ import com .mapbox .geojson .gson .PointDeserializer ;
1415
1516import java .util .List ;
1617
@@ -289,10 +290,11 @@ public static TypeAdapter<RouteOptions> typeAdapter(Gson gson) {
289290 * method
290291 * @since 3.4.0
291292 */
292- public static RouteLeg fromJson (String json ) {
293+ public static RouteOptions fromJson (String json ) {
293294 GsonBuilder gson = new GsonBuilder ();
294295 gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
295- return gson .create ().fromJson (json , RouteLeg .class );
296+ gson .registerTypeAdapter (Point .class , new PointDeserializer ());
297+ return gson .create ().fromJson (json , RouteOptions .class );
296298 }
297299
298300 /**
Original file line number Diff line number Diff line change @@ -190,4 +190,28 @@ public void directionsRequestResult_doesContainOverview() throws Exception {
190190
191191 assertEquals (DirectionsCriteria .OVERVIEW_SIMPLIFIED , route .routeOptions ().overview ());
192192 }
193+
194+ @ Test
195+ public void toJson_fromJson () throws Exception {
196+ Response <DirectionsResponse > response = MapboxDirections .builder ()
197+ .geometries (DirectionsCriteria .GEOMETRY_POLYLINE6 )
198+ .baseUrl (mockUrl .toString ())
199+ .accessToken (ACCESS_TOKEN )
200+ .origin (Point .fromLngLat (1.0 , 1.0 ))
201+ .destination (Point .fromLngLat (5.0 , 5.0 ))
202+ .profile (DirectionsCriteria .PROFILE_WALKING )
203+ .continueStraight (false )
204+ .language (Locale .CANADA )
205+ .overview (DirectionsCriteria .OVERVIEW_SIMPLIFIED )
206+ .alternatives (true )
207+ .build ()
208+ .executeCall ();
209+
210+ RouteOptions routeOptions = response .body ().routes ().get (0 ).routeOptions ();
211+
212+ String jsonString = routeOptions .toJson ();
213+ RouteOptions routeOptionsFromJson = RouteOptions .fromJson (jsonString );
214+
215+ assertEquals (routeOptions , routeOptionsFromJson );
216+ }
193217}
You can’t perform that action at this time.
0 commit comments