From 27efedeb36bb79246edce2e6e68168505dc3d9ae Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Mon, 26 Mar 2018 19:54:31 +0300 Subject: [PATCH 1/2] Update java level to 1.8 --- build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index 2d4e547b1..9ef8bc98b 100644 --- a/build.gradle +++ b/build.gradle @@ -49,8 +49,8 @@ subprojects { } } - sourceCompatibility = "1.7" - targetCompatibility = "1.7" + sourceCompatibility = "1.8" + targetCompatibility = "1.8" dependencies { From 8279e6f1e66e6464a1fd4e5011385a06713d31e7 Mon Sep 17 00:00:00 2001 From: Ivo van Dongen Date: Mon, 26 Mar 2018 19:54:58 +0300 Subject: [PATCH 2/2] [geojson] Add static method to load Geometry form json string --- .../java/com/mapbox/geojson/Geometry.java | 23 +++++++++++++++++++ .../java/com/mapbox/geojson/GeometryTest.java | 21 +++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 services-geojson/src/test/java/com/mapbox/geojson/GeometryTest.java diff --git a/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java b/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java index ad792377f..ce1f418a4 100644 --- a/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java +++ b/services-geojson/src/main/java/com/mapbox/geojson/Geometry.java @@ -1,5 +1,12 @@ 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. @@ -8,4 +15,20 @@ */ 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); + } + } diff --git a/services-geojson/src/test/java/com/mapbox/geojson/GeometryTest.java b/services-geojson/src/test/java/com/mapbox/geojson/GeometryTest.java new file mode 100644 index 000000000..3d9fd0dd2 --- /dev/null +++ b/services-geojson/src/test/java/com/mapbox/geojson/GeometryTest.java @@ -0,0 +1,21 @@ +package com.mapbox.geojson; + +import com.mapbox.core.TestUtils; + +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +public class GeometryTest extends TestUtils { + + private static final String SAMPLE_GEOMETRY_COLLECTION = "sample-geometrycollection.json"; + + @Test + public void fromJson() throws IOException { + final String json = loadJsonFixture(SAMPLE_GEOMETRY_COLLECTION); + Geometry geo = Geometry.fromJson(json); + assertEquals(geo.type(), "GeometryCollection"); + } +}