99import com .mapbox .api .directions .v5 .DirectionsAdapterFactory ;
1010import com .mapbox .geojson .Point ;
1111import com .mapbox .geojson .PointAsCoordinatesTypeAdapter ;
12+
13+ import java .io .Reader ;
1214import java .util .ArrayList ;
1315import java .util .List ;
1416
@@ -139,11 +141,25 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
139141 * @since 3.0.0
140142 */
141143 public static DirectionsResponse fromJson (@ NonNull String json ) {
142- GsonBuilder gson = new GsonBuilder ();
143- gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
144- gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
145144 // rebuilding to ensure that underlying routes have assigned indices and UUID
146- return gson .create ().fromJson (json , DirectionsResponse .class ).toBuilder ().build ();
145+ return createGson ().fromJson (json , DirectionsResponse .class ).toBuilder ().build ();
146+ }
147+
148+ /**
149+ * Deserializes a new instance of this class reading from the specified reader.
150+ * <p>
151+ * Consider using {@link #fromJson(Reader, RouteOptions)} if the result is used with
152+ * downstream consumers of the directions models (like Mapbox Navigation SDK)
153+ * to provide rerouting and route refreshing features.
154+ *
155+ * @param json a reader producing a valid JSON defining a GeoJson Directions Response
156+ * @return a new instance of this class defined by the values passed inside this static factory
157+ * method
158+ * @see #fromJson(Reader, RouteOptions)
159+ */
160+ public static DirectionsResponse fromJson (@ NonNull Reader json ) {
161+ // rebuilding to ensure that underlying routes have assigned indices and UUID
162+ return createGson ().fromJson (json , DirectionsResponse .class ).toBuilder ().build ();
147163 }
148164
149165 /**
@@ -166,10 +182,7 @@ public static DirectionsResponse fromJson(@NonNull String json) {
166182 @ Deprecated
167183 public static DirectionsResponse fromJson (
168184 @ NonNull String json , @ Nullable RouteOptions routeOptions , @ Nullable String requestUuid ) {
169- GsonBuilder gson = new GsonBuilder ();
170- gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
171- gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
172- DirectionsResponse response = gson .create ().fromJson (json , DirectionsResponse .class );
185+ DirectionsResponse response = createGson ().fromJson (json , DirectionsResponse .class );
173186 if (routeOptions != null ) {
174187 response = response .updateWithRequestData (routeOptions );
175188 }
@@ -196,11 +209,33 @@ public static DirectionsResponse fromJson(
196209 * @see RouteOptions#fromJson(String)
197210 */
198211 public static DirectionsResponse fromJson (
199- @ NonNull String json , @ NonNull RouteOptions routeOptions ) {
200- GsonBuilder gson = new GsonBuilder ();
201- gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
202- gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
203- DirectionsResponse response = gson .create ().fromJson (json , DirectionsResponse .class );
212+ @ NonNull String json ,
213+ @ NonNull RouteOptions routeOptions
214+ ) {
215+ DirectionsResponse response = createGson ().fromJson (json , DirectionsResponse .class );
216+ // rebuilding to ensure that underlying routes have assigned indices and UUID
217+ return response .updateWithRequestData (routeOptions );
218+ }
219+
220+ /**
221+ * Deserializes a new instance of this class reading from the specified reader.
222+ * <p>
223+ * The parameter of {@link RouteOptions} that were used to make the original route request
224+ * which might be required by downstream consumers of the directions models
225+ * (like Mapbox Navigation SDK) to provide rerouting and route refreshing features.
226+ *
227+ * @param json a reader producing a valid JSON defining a GeoJson Directions Response
228+ * @param routeOptions options that were used during the original route request
229+ * @return a new instance of this class defined by the values passed inside this static factory
230+ * method
231+ * @see RouteOptions#fromUrl(java.net.URL)
232+ * @see RouteOptions#fromJson(String)
233+ */
234+ public static DirectionsResponse fromJson (
235+ @ NonNull Reader json ,
236+ @ NonNull RouteOptions routeOptions
237+ ) {
238+ DirectionsResponse response = createGson ().fromJson (json , DirectionsResponse .class );
204239 // rebuilding to ensure that underlying routes have assigned indices and UUID
205240 return response .updateWithRequestData (routeOptions );
206241 }
@@ -332,4 +367,11 @@ public DirectionsResponse build() {
332367 return autoBuild ();
333368 }
334369 }
370+
371+ @ NonNull private static Gson createGson () {
372+ GsonBuilder gson = new GsonBuilder ();
373+ gson .registerTypeAdapterFactory (DirectionsAdapterFactory .create ());
374+ gson .registerTypeAdapter (Point .class , new PointAsCoordinatesTypeAdapter ());
375+ return gson .create ();
376+ }
335377}
0 commit comments