From 850ae226b7e9aa19953b7171eaa608728b58474f Mon Sep 17 00:00:00 2001 From: danesfeder Date: Tue, 16 Jan 2018 17:01:57 -0500 Subject: [PATCH] Add fromJson method to DirectionsRoute --- .../directions/v5/models/DirectionsRoute.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java b/services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java index a0309f676..e0c6f51d7 100644 --- a/services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java +++ b/services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java @@ -1,10 +1,20 @@ package com.mapbox.api.directions.v5.models; import android.support.annotation.Nullable; + import com.google.auto.value.AutoValue; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.TypeAdapter; import com.google.gson.annotations.SerializedName; +import com.mapbox.api.directions.v5.DirectionsAdapterFactory; +import com.mapbox.geojson.BoundingBox; +import com.mapbox.geojson.Geometry; +import com.mapbox.geojson.Point; +import com.mapbox.geojson.gson.BoundingBoxDeserializer; +import com.mapbox.geojson.gson.GeoJsonAdapterFactory; +import com.mapbox.geojson.gson.GeometryDeserializer; +import com.mapbox.geojson.gson.PointDeserializer; import java.io.Serializable; import java.util.List; @@ -27,6 +37,24 @@ public static Builder builder() { return new AutoValue_DirectionsRoute.Builder(); } + /** + * Create a new instance of this class by passing in a formatted valid JSON String. + * + * @param json a formatted valid JSON string defining a GeoJson Directions Route + * @return a new instance of this class defined by the values passed inside this static factory + * method + * @since 3.0.0 + */ + public static DirectionsRoute fromJson(String json) { + GsonBuilder gson = new GsonBuilder(); + gson.registerTypeAdapter(Point.class, new PointDeserializer()); + gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); + gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); + gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); + gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create()); + return gson.create().fromJson(json, DirectionsRoute.class); + } + /** * The distance traveled from origin to destination. *