Skip to content

Commit 4d1b2f8

Browse files
authored
move default method to a utility class so that we can downgrade to java 7 (#876)
1 parent b7d92fb commit 4d1b2f8

5 files changed

Lines changed: 37 additions & 27 deletions

File tree

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ subprojects {
4949
}
5050
}
5151

52-
sourceCompatibility = "1.8"
53-
targetCompatibility = "1.8"
52+
sourceCompatibility = JavaVersion.VERSION_1_7
53+
targetCompatibility = JavaVersion.VERSION_1_7
5454

5555
dependencies {
5656

services-directions/src/test/java/com/mapbox/api/directions/v5/models/RouteLegTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public void testToFromJson1() {
7171
"heavy",
7272
"heavy");
7373

74+
List<LegStep> steps = new ArrayList<>();
75+
7476
LegAnnotation annotation = LegAnnotation.builder()
7577
.congestion(new ArrayList<String>())
7678
.distance(distanceList)
@@ -84,7 +86,7 @@ public void testToFromJson1() {
8486
.distance(53.4)
8587
//.weight(14.3)
8688
.duration(14.3)
87-
.steps(new ArrayList<>())
89+
.steps(steps)
8890
.summary("")
8991
.build();
9092

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
package com.mapbox.geojson;
22

3-
import android.support.annotation.NonNull;
4-
5-
import com.google.gson.GsonBuilder;
6-
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
7-
import com.mapbox.geojson.gson.GeometryDeserializer;
8-
import com.mapbox.geojson.gson.PointDeserializer;
9-
103
/**
114
* Each of the six geometries and {@link GeometryCollection}
125
* which make up GeoJson implement this interface.
@@ -15,20 +8,4 @@
158
*/
169
public interface Geometry extends GeoJson {
1710

18-
/**
19-
* Create a new instance of this class by passing in a formatted valid JSON String.
20-
*
21-
* @param json a formatted valid JSON string defining a GeoJson Geometry
22-
* @return a new instance of this class defined by the values passed inside this static factory
23-
* method
24-
* @since 3.0.0
25-
*/
26-
static Geometry fromJson(@NonNull String json) {
27-
GsonBuilder gson = new GsonBuilder();
28-
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
29-
gson.registerTypeAdapter(Point.class, new PointDeserializer());
30-
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
31-
return gson.create().fromJson(json, Geometry.class);
32-
}
33-
3411
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.mapbox.geojson.gson;
2+
3+
import android.support.annotation.NonNull;
4+
5+
import com.google.gson.GsonBuilder;
6+
import com.mapbox.geojson.Geometry;
7+
import com.mapbox.geojson.Point;
8+
9+
/**
10+
* This is a utility class that helps create a Geometry instance from a JSON string.
11+
* @since 3.5.0
12+
*/
13+
public class GeometryGeoJson {
14+
15+
/**
16+
* Create a new instance of Geometry class by passing in a formatted valid JSON String.
17+
*
18+
* @param json a formatted valid JSON string defining a GeoJson Geometry
19+
* @return a new instance of Geometry class defined by the values passed inside
20+
* this static factory method
21+
* @since 3.5.0
22+
*/
23+
public static Geometry fromJson(@NonNull String json) {
24+
GsonBuilder gson = new GsonBuilder();
25+
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
26+
gson.registerTypeAdapter(Point.class, new PointDeserializer());
27+
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
28+
return gson.create().fromJson(json, Geometry.class);
29+
}
30+
}

services-geojson/src/test/java/com/mapbox/geojson/GeometryTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.mapbox.geojson;
22

33
import com.mapbox.core.TestUtils;
4+
import com.mapbox.geojson.gson.GeometryGeoJson;
45

56
import org.junit.Test;
67

@@ -15,7 +16,7 @@ public class GeometryTest extends TestUtils {
1516
@Test
1617
public void fromJson() throws IOException {
1718
final String json = loadJsonFixture(SAMPLE_GEOMETRY_COLLECTION);
18-
Geometry geo = Geometry.fromJson(json);
19+
Geometry geo = GeometryGeoJson.fromJson(json);
1920
assertEquals(geo.type(), "GeometryCollection");
2021
}
2122
}

0 commit comments

Comments
 (0)