11package com .mapbox .api .directions .v5 .models ;
22
33import android .support .annotation .Nullable ;
4+
45import com .google .auto .value .AutoValue ;
56import com .google .gson .Gson ;
7+ import com .google .gson .GsonBuilder ;
68import com .google .gson .TypeAdapter ;
79import com .google .gson .annotations .SerializedName ;
10+ import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
11+ import com .mapbox .geojson .BoundingBox ;
12+ import com .mapbox .geojson .Geometry ;
13+ import com .mapbox .geojson .Point ;
14+ import com .mapbox .geojson .gson .BoundingBoxDeserializer ;
15+ import com .mapbox .geojson .gson .GeoJsonAdapterFactory ;
16+ import com .mapbox .geojson .gson .GeometryDeserializer ;
17+ import com .mapbox .geojson .gson .PointDeserializer ;
818
919import java .io .Serializable ;
1020import java .util .List ;
@@ -27,6 +37,24 @@ public static Builder builder() {
2737 return new AutoValue_DirectionsRoute .Builder ();
2838 }
2939
40+ /**
41+ * Create a new instance of this class by passing in a formatted valid JSON String.
42+ *
43+ * @param json a formatted valid JSON string defining a GeoJson Directions Route
44+ * @return a new instance of this class defined by the values passed inside this static factory
45+ * method
46+ * @since 3.0.0
47+ */
48+ public static DirectionsRoute fromJson (String json ) {
49+ GsonBuilder gson = new GsonBuilder ();
50+ gson .registerTypeAdapter (Point .class , new PointDeserializer ());
51+ gson .registerTypeAdapter (Geometry .class , new GeometryDeserializer ());
52+ gson .registerTypeAdapter (BoundingBox .class , new BoundingBoxDeserializer ());
53+ gson .registerTypeAdapterFactory (GeoJsonAdapterFactory .create ());
54+ gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
55+ return gson .create ().fromJson (json , DirectionsRoute .class );
56+ }
57+
3058 /**
3159 * The distance traveled from origin to destination.
3260 *
0 commit comments