Skip to content

Commit

Permalink
move default method to a utility class so that we can downgrade to ja…
Browse files Browse the repository at this point in the history
…va 7
  • Loading branch information
osana committed Sep 19, 2018
1 parent b7d92fb commit 78203a3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Expand Up @@ -49,8 +49,8 @@ subprojects {
}
}

sourceCompatibility = "1.8"
targetCompatibility = "1.8"
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {

Expand Down
Expand Up @@ -71,6 +71,8 @@ public void testToFromJson1() {
"heavy",
"heavy");

List<LegStep> steps = new ArrayList<>();

LegAnnotation annotation = LegAnnotation.builder()
.congestion(new ArrayList<String>())
.distance(distanceList)
Expand All @@ -84,7 +86,7 @@ public void testToFromJson1() {
.distance(53.4)
//.weight(14.3)
.duration(14.3)
.steps(new ArrayList<>())
.steps(steps)
.summary("")
.build();

Expand Down
23 changes: 0 additions & 23 deletions services-geojson/src/main/java/com/mapbox/geojson/Geometry.java
@@ -1,12 +1,5 @@
package com.mapbox.geojson;

import android.support.annotation.NonNull;

import com.google.gson.GsonBuilder;
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
import com.mapbox.geojson.gson.GeometryDeserializer;
import com.mapbox.geojson.gson.PointDeserializer;

/**
* Each of the six geometries and {@link GeometryCollection}
* which make up GeoJson implement this interface.
Expand All @@ -15,20 +8,4 @@
*/
public interface Geometry extends GeoJson {

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

}
@@ -0,0 +1,30 @@
package com.mapbox.geojson.gson;

import android.support.annotation.NonNull;

import com.google.gson.GsonBuilder;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.Point;

/**
* This is a utility class that helps create a Geometry instance from a JSON string.
* @since 3.5.0
*/
public class GeometryGeoJson {

/**
* Create a new instance of Geometry class by passing in a formatted valid JSON String.
*
* @param json a formatted valid JSON string defining a GeoJson Geometry
* @return a new instance of Geometry class defined by the values passed inside
* this static factory method
* @since 3.5.0
*/
static Geometry fromJson(@NonNull String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
gson.registerTypeAdapter(Point.class, new PointDeserializer());
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
return gson.create().fromJson(json, Geometry.class);
}
}
@@ -1,6 +1,7 @@
package com.mapbox.geojson;

import com.mapbox.core.TestUtils;
import com.mapbox.geojson.gson.GeometryGeoJson;

import org.junit.Test;

Expand All @@ -15,7 +16,7 @@ public class GeometryTest extends TestUtils {
@Test
public void fromJson() throws IOException {
final String json = loadJsonFixture(SAMPLE_GEOMETRY_COLLECTION);
Geometry geo = Geometry.fromJson(json);
Geometry geo = GeometryGeoJson.fromJson(json);
assertEquals(geo.type(), "GeometryCollection");
}
}

0 comments on commit 78203a3

Please sign in to comment.