Skip to content

Commit

Permalink
added toJson and fromJson to Directions models (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
osana authored Jul 24, 2018
1 parent e25b6c9 commit 0dfbee7
Show file tree
Hide file tree
Showing 29 changed files with 900 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

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.api.directions.v5.MapboxDirections;

import java.io.Serializable;
import java.util.List;

/**
Expand All @@ -20,7 +21,8 @@
* @since 3.0.0
*/
@AutoValue
public abstract class BannerComponents implements Serializable, Comparable<BannerComponents> {
public abstract class BannerComponents extends DirectionsJsonObject
implements Comparable<BannerComponents> {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand Down Expand Up @@ -147,6 +149,20 @@ public static TypeAdapter<BannerComponents> typeAdapter(Gson gson) {
return new AutoValue_BannerComponents.GsonTypeAdapter(gson);
}

/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a BannerComponents
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.4.0
*/
public static BannerComponents fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, BannerComponents.class);
}

/**
* Allows ability to sort/compare by abbreviation priority. This is null-safe for values of
* abbreviationPriority, and treats BannerComponents with a null abreviationPriority as having an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.MapboxDirections;

import java.io.Serializable;

/**
* Visual instruction information related to a particular {@link LegStep} useful for making UI
* elements inside your application such as banners. To receive this information, your request must
Expand All @@ -16,7 +16,7 @@
* @since 3.0.0
*/
@AutoValue
public abstract class BannerInstructions implements Serializable {
public abstract class BannerInstructions extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand Down Expand Up @@ -91,6 +91,21 @@ public static TypeAdapter<BannerInstructions> typeAdapter(Gson gson) {
return new AutoValue_BannerInstructions.GsonTypeAdapter(gson);
}


/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a BannerInstructions
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.4.0
*/
public static BannerInstructions fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, BannerInstructions.class);
}

/**
* This builder can be used to set the values describing the {@link BannerInstructions}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

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.api.directions.v5.MapboxDirections;

import java.io.Serializable;
import java.util.List;

/**
Expand All @@ -20,7 +21,7 @@
* @since 3.0.0
*/
@AutoValue
public abstract class BannerText implements Serializable {
public abstract class BannerText extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand Down Expand Up @@ -136,6 +137,20 @@ public static TypeAdapter<BannerText> typeAdapter(Gson gson) {
return new AutoValue_BannerText.GsonTypeAdapter(gson);
}

/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a BannerText
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.4.0
*/
public static BannerText fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, BannerText.class);
}

/**
* This builder can be used to set the values describing the {@link BannerText}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mapbox.api.directions.v5.models;

import com.google.gson.GsonBuilder;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;

import java.io.Serializable;

/**
* Provideds a base class for Directions model classes.
*
* @since 3.4.0
*/
public class DirectionsJsonObject implements Serializable {

/**
* This takes the currently defined values found inside this instance and converts it to a json
* string.
*
* @return a JSON string which represents this DirectionsJsonObject
* @since 3.4.0
*/
public String toJson() {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().toJson(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,7 @@
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
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;

/**
Expand All @@ -27,7 +19,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class DirectionsResponse implements Serializable {
public abstract class DirectionsResponse extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand All @@ -40,24 +32,6 @@ public static Builder builder() {
return new AutoValue_DirectionsResponse.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 Response
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.0.0
*/
public static DirectionsResponse 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, DirectionsResponse.class);
}

/**
* String indicating the state of the response. This is a separate code than the HTTP status code.
* On normal valid responses, the value will be Ok. The possible responses are listed below:
Expand Down Expand Up @@ -141,6 +115,20 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
return new AutoValue_DirectionsResponse.GsonTypeAdapter(gson);
}

/**
* 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 Response
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.0.0
*/
public static DirectionsResponse fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, DirectionsResponse.class);
}

/**
* This builder can be used to set the values describing the {@link DirectionsResponse}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@
import com.google.gson.annotations.SerializedName;
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
import com.mapbox.api.directions.v5.MapboxDirections;
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;

/**
Expand All @@ -26,7 +18,7 @@
* @since 1.0.0
*/
@AutoValue
public abstract class DirectionsRoute implements Serializable {
public abstract class DirectionsRoute extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand All @@ -38,24 +30,6 @@ 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.
*
Expand Down Expand Up @@ -159,6 +133,20 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
return new AutoValue_DirectionsRoute.GsonTypeAdapter(gson);
}

/**
* 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.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, DirectionsRoute.class);
}

/**
* This builder can be used to set the values describing the {@link DirectionsRoute}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
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.Point;

import java.io.Serializable;

/**
* An input coordinate snapped to the roads network.
*
* @since 1.0.0
*/
@AutoValue
public abstract class DirectionsWaypoint implements Serializable {
public abstract class DirectionsWaypoint extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand Down Expand Up @@ -80,6 +80,20 @@ public static TypeAdapter<DirectionsWaypoint> typeAdapter(Gson gson) {
return new AutoValue_DirectionsWaypoint.GsonTypeAdapter(gson);
}

/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a DirectionsWaypoint
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.4.0
*/
public static DirectionsWaypoint fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, DirectionsWaypoint.class);
}

/**
* This builder can be used to set the values describing the {@link DirectionsWaypoint}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
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.mapbox.api.directions.v5.DirectionsAdapterFactory;

import java.io.Serializable;
import java.util.List;

/**
Expand All @@ -14,7 +15,7 @@
* @since 2.0.0
*/
@AutoValue
public abstract class IntersectionLanes implements Serializable {
public abstract class IntersectionLanes extends DirectionsJsonObject {

/**
* Create a new instance of this class by using the {@link Builder} class.
Expand Down Expand Up @@ -71,6 +72,20 @@ public static TypeAdapter<IntersectionLanes> typeAdapter(Gson gson) {
return new AutoValue_IntersectionLanes.GsonTypeAdapter(gson);
}

/**
* Create a new instance of this class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining an IntersectionLanes
* @return a new instance of this class defined by the values passed inside this static factory
* method
* @since 3.4.0
*/
public static IntersectionLanes fromJson(String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
return gson.create().fromJson(json, IntersectionLanes.class);
}

/**
* This builder can be used to set the values describing the {@link IntersectionLanes}.
*
Expand Down
Loading

0 comments on commit 0dfbee7

Please sign in to comment.