55import com .google .auto .value .AutoValue ;
66import com .google .gson .Gson ;
77import com .google .gson .GsonBuilder ;
8+ import com .google .gson .JsonObject ;
89import com .google .gson .TypeAdapter ;
910import com .google .gson .annotations .SerializedName ;
1011import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
@@ -165,26 +166,66 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
165166
166167 /**
167168 * Create a new instance of this class by passing in a formatted valid JSON String.
169+ * <p>
170+ * Use this method if the provided serialized route was not obtained by this library.
171+ * Alternatively, use {@link #fromJson(String, String)}.
172+ * <p>
173+ * If you're using the provided route with the Mapbox Navigation SDK, also see
174+ * {@link #fromJson(String, RouteOptions, String)}.
168175 *
169176 * @param json a formatted valid JSON string defining a GeoJson Directions Route
170177 * @return a new instance of this class defined by the values passed inside this static factory
171178 * method
172- * @see #fromJson(String, RouteOptions, String)
173- * @since 3.0.0
174179 */
175180 public static DirectionsRoute fromJson (@ NonNull String json ) {
176181 GsonBuilder gson = new GsonBuilder ();
182+ JsonObject jsonObject = gson .create ().fromJson (json , JsonObject .class );
183+ if (jsonObject .has ("routeOptions" )) {
184+ throw new IllegalArgumentException (
185+ "Provided serialized route contains RouteOptions. "
186+ + "Use DirectionsRoute#fromJson(json, accessToken) instead."
187+ );
188+ }
189+ gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
190+ gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
191+ return gson .create ().fromJson (jsonObject , DirectionsRoute .class );
192+ }
193+
194+ /**
195+ * Create a new instance of this class by passing in a formatted valid JSON String.
196+ * <p>
197+ * Use this method if the provided serialized route was obtained by this library.
198+ * This means that it includes {@link RouteOptions} and you need to supply a Mapbox Access Token.
199+ * Alternatively, use {@link #fromJson(String)}.
200+ *
201+ * @param json a formatted valid JSON string defining a GeoJson Directions Route
202+ * @param accessToken a Mapbox Access Token
203+ * @return a new instance of this class defined by the values passed inside this static factory
204+ * method
205+ */
206+ public static DirectionsRoute fromJson (@ NonNull String json , @ NonNull String accessToken ) {
207+ GsonBuilder gson = new GsonBuilder ();
208+ JsonObject jsonObject = gson .create ().fromJson (json , JsonObject .class );
209+ if (jsonObject .has ("routeOptions" )) {
210+ JsonObject routeOptions = jsonObject .getAsJsonObject ("routeOptions" );
211+ routeOptions .addProperty ("access_token" , accessToken );
212+ } else {
213+ throw new IllegalArgumentException (
214+ "Provided serialized route does not contain RouteOptions. "
215+ + "Use DirectionsRoute#fromJson(json) instead."
216+ );
217+ }
177218 gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
178219 gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
179- return gson .create ().fromJson (json , DirectionsRoute .class );
220+ return gson .create ().fromJson (jsonObject , DirectionsRoute .class );
180221 }
181222
182223 /**
183224 * Create a new instance of this class by passing in a formatted valid JSON String.
184225 * <p>
185226 * The parameters of {@link RouteOptions} that were used to make the original route request
186227 * as well as the {@link String} UUID of the original response are needed
187- * by the Navigation SDK to support correct rerouting and route refreshing.
228+ * by the Mapbox Navigation SDK to support correct rerouting and route refreshing.
188229 *
189230 * @param json a formatted valid JSON string defining a GeoJson Directions Route
190231 * @param routeOptions options that were used during the original route request
@@ -198,10 +239,7 @@ public static DirectionsRoute fromJson(@NonNull String json) {
198239 public static DirectionsRoute fromJson (
199240 @ NonNull String json , @ Nullable RouteOptions routeOptions , @ Nullable String requestUuid
200241 ) {
201- GsonBuilder gson = new GsonBuilder ();
202- gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
203- gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
204- return gson .create ().fromJson (json , DirectionsRoute .class )
242+ return fromJson (json )
205243 .toBuilder ()
206244 .routeOptions (routeOptions )
207245 .requestUuid (requestUuid )
@@ -223,6 +261,7 @@ public abstract static class Builder {
223261 * @return this builder for chaining options together
224262 * @since 3.0.0
225263 */
264+ @ NonNull
226265 public abstract Builder distance (@ NonNull Double distance );
227266
228267 /**
@@ -232,6 +271,7 @@ public abstract static class Builder {
232271 * @return this builder for chaining options together
233272 * @since 3.0.0
234273 */
274+ @ NonNull
235275 public abstract Builder duration (@ NonNull Double duration );
236276
237277 /**
@@ -244,6 +284,7 @@ public abstract static class Builder {
244284 * @return this builder for chaining options together
245285 * @since 5.5.0
246286 */
287+ @ NonNull
247288 public abstract Builder durationTypical (@ Nullable Double durationTypical );
248289
249290 /**
@@ -253,6 +294,7 @@ public abstract static class Builder {
253294 * @return this builder for chaining options together
254295 * @since 3.0.0
255296 */
297+ @ NonNull
256298 public abstract Builder geometry (@ Nullable String geometry );
257299
258300 /**
@@ -262,6 +304,7 @@ public abstract static class Builder {
262304 * @return this builder for chaining options together
263305 * @since 3.0.0
264306 */
307+ @ NonNull
265308 public abstract Builder weight (@ Nullable Double weight );
266309
267310 /**
@@ -273,6 +316,7 @@ public abstract static class Builder {
273316 * @return this builder for chaining options together
274317 * @since 3.0.0
275318 */
319+ @ NonNull
276320 public abstract Builder weightName (@ Nullable String weightName );
277321
278322 /**
@@ -282,6 +326,7 @@ public abstract static class Builder {
282326 * @return this builder for chaining options together
283327 * @since 3.0.0
284328 */
329+ @ NonNull
285330 public abstract Builder legs (@ Nullable List <RouteLeg > legs );
286331
287332 /**
@@ -292,6 +337,7 @@ public abstract static class Builder {
292337 * @return this builder for chaining options together
293338 * @since 3.0.0
294339 */
340+ @ NonNull
295341 public abstract Builder routeOptions (@ Nullable RouteOptions routeOptions );
296342
297343 /**
@@ -302,6 +348,7 @@ public abstract static class Builder {
302348 * @return this builder for chaining options together
303349 * @since 3.1.0
304350 */
351+ @ NonNull
305352 public abstract Builder voiceLanguage (@ Nullable String voiceLanguage );
306353
307354 /**
@@ -313,6 +360,7 @@ public abstract static class Builder {
313360 @ NonNull
314361 public abstract Builder requestUuid (@ Nullable String requestUuid );
315362
363+ @ NonNull
316364 abstract Builder routeIndex (String routeIndex );
317365
318366 /**
@@ -321,6 +369,7 @@ public abstract static class Builder {
321369 * @return a new {@link DirectionsRoute} using the provided values in this builder
322370 * @since 3.0.0
323371 */
372+ @ NonNull
324373 public abstract DirectionsRoute build ();
325374 }
326375}
0 commit comments