Skip to content

Commit 0dfbee7

Browse files
authored
added toJson and fromJson to Directions models (#854)
1 parent e25b6c9 commit 0dfbee7

29 files changed

Lines changed: 900 additions & 85 deletions

services-directions/src/main/java/com/mapbox/api/directions/v5/models/BannerComponents.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import com.google.auto.value.AutoValue;
66
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
78
import com.google.gson.TypeAdapter;
89
import com.google.gson.annotations.SerializedName;
10+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
911
import com.mapbox.api.directions.v5.MapboxDirections;
1012

11-
import java.io.Serializable;
1213
import java.util.List;
1314

1415
/**
@@ -20,7 +21,8 @@
2021
* @since 3.0.0
2122
*/
2223
@AutoValue
23-
public abstract class BannerComponents implements Serializable, Comparable<BannerComponents> {
24+
public abstract class BannerComponents extends DirectionsJsonObject
25+
implements Comparable<BannerComponents> {
2426

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

152+
/**
153+
* Create a new instance of this class by passing in a formatted valid JSON String.
154+
*
155+
* @param json a formatted valid JSON string defining a BannerComponents
156+
* @return a new instance of this class defined by the values passed inside this static factory
157+
* method
158+
* @since 3.4.0
159+
*/
160+
public static BannerComponents fromJson(String json) {
161+
GsonBuilder gson = new GsonBuilder();
162+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
163+
return gson.create().fromJson(json, BannerComponents.class);
164+
}
165+
150166
/**
151167
* Allows ability to sort/compare by abbreviation priority. This is null-safe for values of
152168
* abbreviationPriority, and treats BannerComponents with a null abreviationPriority as having an

services-directions/src/main/java/com/mapbox/api/directions/v5/models/BannerInstructions.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
import android.support.annotation.Nullable;
44
import com.google.auto.value.AutoValue;
55
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
67
import com.google.gson.TypeAdapter;
8+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
79
import com.mapbox.api.directions.v5.MapboxDirections;
810

9-
import java.io.Serializable;
10-
1111
/**
1212
* Visual instruction information related to a particular {@link LegStep} useful for making UI
1313
* elements inside your application such as banners. To receive this information, your request must
@@ -16,7 +16,7 @@
1616
* @since 3.0.0
1717
*/
1818
@AutoValue
19-
public abstract class BannerInstructions implements Serializable {
19+
public abstract class BannerInstructions extends DirectionsJsonObject {
2020

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

94+
95+
/**
96+
* Create a new instance of this class by passing in a formatted valid JSON String.
97+
*
98+
* @param json a formatted valid JSON string defining a BannerInstructions
99+
* @return a new instance of this class defined by the values passed inside this static factory
100+
* method
101+
* @since 3.4.0
102+
*/
103+
public static BannerInstructions fromJson(String json) {
104+
GsonBuilder gson = new GsonBuilder();
105+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
106+
return gson.create().fromJson(json, BannerInstructions.class);
107+
}
108+
94109
/**
95110
* This builder can be used to set the values describing the {@link BannerInstructions}.
96111
*

services-directions/src/main/java/com/mapbox/api/directions/v5/models/BannerText.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import com.google.auto.value.AutoValue;
66
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
78
import com.google.gson.TypeAdapter;
89
import com.google.gson.annotations.SerializedName;
10+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
911
import com.mapbox.api.directions.v5.MapboxDirections;
1012

11-
import java.io.Serializable;
1213
import java.util.List;
1314

1415
/**
@@ -20,7 +21,7 @@
2021
* @since 3.0.0
2122
*/
2223
@AutoValue
23-
public abstract class BannerText implements Serializable {
24+
public abstract class BannerText extends DirectionsJsonObject {
2425

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

140+
/**
141+
* Create a new instance of this class by passing in a formatted valid JSON String.
142+
*
143+
* @param json a formatted valid JSON string defining a BannerText
144+
* @return a new instance of this class defined by the values passed inside this static factory
145+
* method
146+
* @since 3.4.0
147+
*/
148+
public static BannerText fromJson(String json) {
149+
GsonBuilder gson = new GsonBuilder();
150+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
151+
return gson.create().fromJson(json, BannerText.class);
152+
}
153+
139154
/**
140155
* This builder can be used to set the values describing the {@link BannerText}.
141156
*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.mapbox.api.directions.v5.models;
2+
3+
import com.google.gson.GsonBuilder;
4+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
5+
6+
import java.io.Serializable;
7+
8+
/**
9+
* Provideds a base class for Directions model classes.
10+
*
11+
* @since 3.4.0
12+
*/
13+
public class DirectionsJsonObject implements Serializable {
14+
15+
/**
16+
* This takes the currently defined values found inside this instance and converts it to a json
17+
* string.
18+
*
19+
* @return a JSON string which represents this DirectionsJsonObject
20+
* @since 3.4.0
21+
*/
22+
public String toJson() {
23+
GsonBuilder gson = new GsonBuilder();
24+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
25+
return gson.create().toJson(this);
26+
}
27+
}

services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsResponse.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@
77
import com.google.gson.GsonBuilder;
88
import com.google.gson.TypeAdapter;
99
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
10-
import com.mapbox.geojson.BoundingBox;
11-
import com.mapbox.geojson.Geometry;
12-
import com.mapbox.geojson.Point;
13-
import com.mapbox.geojson.gson.BoundingBoxDeserializer;
14-
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
15-
import com.mapbox.geojson.gson.GeometryDeserializer;
16-
import com.mapbox.geojson.gson.PointDeserializer;
17-
18-
import java.io.Serializable;
10+
1911
import java.util.List;
2012

2113
/**
@@ -27,7 +19,7 @@
2719
* @since 1.0.0
2820
*/
2921
@AutoValue
30-
public abstract class DirectionsResponse implements Serializable {
22+
public abstract class DirectionsResponse extends DirectionsJsonObject {
3123

3224
/**
3325
* Create a new instance of this class by using the {@link Builder} class.
@@ -40,24 +32,6 @@ public static Builder builder() {
4032
return new AutoValue_DirectionsResponse.Builder();
4133
}
4234

43-
/**
44-
* Create a new instance of this class by passing in a formatted valid JSON String.
45-
*
46-
* @param json a formatted valid JSON string defining a GeoJson Directions Response
47-
* @return a new instance of this class defined by the values passed inside this static factory
48-
* method
49-
* @since 3.0.0
50-
*/
51-
public static DirectionsResponse fromJson(String json) {
52-
GsonBuilder gson = new GsonBuilder();
53-
gson.registerTypeAdapter(Point.class, new PointDeserializer());
54-
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
55-
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
56-
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
57-
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
58-
return gson.create().fromJson(json, DirectionsResponse.class);
59-
}
60-
6135
/**
6236
* String indicating the state of the response. This is a separate code than the HTTP status code.
6337
* On normal valid responses, the value will be Ok. The possible responses are listed below:
@@ -141,6 +115,20 @@ public static TypeAdapter<DirectionsResponse> typeAdapter(Gson gson) {
141115
return new AutoValue_DirectionsResponse.GsonTypeAdapter(gson);
142116
}
143117

118+
/**
119+
* Create a new instance of this class by passing in a formatted valid JSON String.
120+
*
121+
* @param json a formatted valid JSON string defining a GeoJson Directions Response
122+
* @return a new instance of this class defined by the values passed inside this static factory
123+
* method
124+
* @since 3.0.0
125+
*/
126+
public static DirectionsResponse fromJson(String json) {
127+
GsonBuilder gson = new GsonBuilder();
128+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
129+
return gson.create().fromJson(json, DirectionsResponse.class);
130+
}
131+
144132
/**
145133
* This builder can be used to set the values describing the {@link DirectionsResponse}.
146134
*

services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsRoute.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@
99
import com.google.gson.annotations.SerializedName;
1010
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
1111
import com.mapbox.api.directions.v5.MapboxDirections;
12-
import com.mapbox.geojson.BoundingBox;
13-
import com.mapbox.geojson.Geometry;
14-
import com.mapbox.geojson.Point;
15-
import com.mapbox.geojson.gson.BoundingBoxDeserializer;
16-
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
17-
import com.mapbox.geojson.gson.GeometryDeserializer;
18-
import com.mapbox.geojson.gson.PointDeserializer;
19-
20-
import java.io.Serializable;
12+
2113
import java.util.List;
2214

2315
/**
@@ -26,7 +18,7 @@
2618
* @since 1.0.0
2719
*/
2820
@AutoValue
29-
public abstract class DirectionsRoute implements Serializable {
21+
public abstract class DirectionsRoute extends DirectionsJsonObject {
3022

3123
/**
3224
* Create a new instance of this class by using the {@link Builder} class.
@@ -38,24 +30,6 @@ public static Builder builder() {
3830
return new AutoValue_DirectionsRoute.Builder();
3931
}
4032

41-
/**
42-
* Create a new instance of this class by passing in a formatted valid JSON String.
43-
*
44-
* @param json a formatted valid JSON string defining a GeoJson Directions Route
45-
* @return a new instance of this class defined by the values passed inside this static factory
46-
* method
47-
* @since 3.0.0
48-
*/
49-
public static DirectionsRoute fromJson(String json) {
50-
GsonBuilder gson = new GsonBuilder();
51-
gson.registerTypeAdapter(Point.class, new PointDeserializer());
52-
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
53-
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
54-
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
55-
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
56-
return gson.create().fromJson(json, DirectionsRoute.class);
57-
}
58-
5933
/**
6034
* The distance traveled from origin to destination.
6135
*
@@ -159,6 +133,20 @@ public static TypeAdapter<DirectionsRoute> typeAdapter(Gson gson) {
159133
return new AutoValue_DirectionsRoute.GsonTypeAdapter(gson);
160134
}
161135

136+
/**
137+
* Create a new instance of this class by passing in a formatted valid JSON String.
138+
*
139+
* @param json a formatted valid JSON string defining a GeoJson Directions Route
140+
* @return a new instance of this class defined by the values passed inside this static factory
141+
* method
142+
* @since 3.0.0
143+
*/
144+
public static DirectionsRoute fromJson(String json) {
145+
GsonBuilder gson = new GsonBuilder();
146+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
147+
return gson.create().fromJson(json, DirectionsRoute.class);
148+
}
149+
162150
/**
163151
* This builder can be used to set the values describing the {@link DirectionsRoute}.
164152
*

services-directions/src/main/java/com/mapbox/api/directions/v5/models/DirectionsWaypoint.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
import android.support.annotation.Nullable;
44
import com.google.auto.value.AutoValue;
55
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
67
import com.google.gson.TypeAdapter;
78
import com.google.gson.annotations.SerializedName;
9+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
810
import com.mapbox.geojson.Point;
911

10-
import java.io.Serializable;
11-
1212
/**
1313
* An input coordinate snapped to the roads network.
1414
*
1515
* @since 1.0.0
1616
*/
1717
@AutoValue
18-
public abstract class DirectionsWaypoint implements Serializable {
18+
public abstract class DirectionsWaypoint extends DirectionsJsonObject {
1919

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

83+
/**
84+
* Create a new instance of this class by passing in a formatted valid JSON String.
85+
*
86+
* @param json a formatted valid JSON string defining a DirectionsWaypoint
87+
* @return a new instance of this class defined by the values passed inside this static factory
88+
* method
89+
* @since 3.4.0
90+
*/
91+
public static DirectionsWaypoint fromJson(String json) {
92+
GsonBuilder gson = new GsonBuilder();
93+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
94+
return gson.create().fromJson(json, DirectionsWaypoint.class);
95+
}
96+
8397
/**
8498
* This builder can be used to set the values describing the {@link DirectionsWaypoint}.
8599
*

services-directions/src/main/java/com/mapbox/api/directions/v5/models/IntersectionLanes.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import android.support.annotation.Nullable;
44
import com.google.auto.value.AutoValue;
55
import com.google.gson.Gson;
6+
import com.google.gson.GsonBuilder;
67
import com.google.gson.TypeAdapter;
8+
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
79

8-
import java.io.Serializable;
910
import java.util.List;
1011

1112
/**
@@ -14,7 +15,7 @@
1415
* @since 2.0.0
1516
*/
1617
@AutoValue
17-
public abstract class IntersectionLanes implements Serializable {
18+
public abstract class IntersectionLanes extends DirectionsJsonObject {
1819

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

75+
/**
76+
* Create a new instance of this class by passing in a formatted valid JSON String.
77+
*
78+
* @param json a formatted valid JSON string defining an IntersectionLanes
79+
* @return a new instance of this class defined by the values passed inside this static factory
80+
* method
81+
* @since 3.4.0
82+
*/
83+
public static IntersectionLanes fromJson(String json) {
84+
GsonBuilder gson = new GsonBuilder();
85+
gson.registerTypeAdapterFactory(DirectionsAdapterFactory.create());
86+
return gson.create().fromJson(json, IntersectionLanes.class);
87+
}
88+
7489
/**
7590
* This builder can be used to set the values describing the {@link IntersectionLanes}.
7691
*

0 commit comments

Comments
 (0)