Skip to content

Commit 642b039

Browse files
ivovandongenCameron Mace
authored andcommitted
Geometry from json (#770)
* Update java level to 1.8 * [geojson] Add static method to load Geometry form json string
1 parent ee98aad commit 642b039

3 files changed

Lines changed: 46 additions & 2 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.7"
53-
targetCompatibility = "1.7"
52+
sourceCompatibility = "1.8"
53+
targetCompatibility = "1.8"
5454

5555
dependencies {
5656

services-geojson/src/main/java/com/mapbox/geojson/Geometry.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
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+
310
/**
411
* Each of the six geometries and {@link GeometryCollection}
512
* which make up GeoJson implement this interface.
@@ -8,4 +15,20 @@
815
*/
916
public interface Geometry extends GeoJson {
1017

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+
1134
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.mapbox.geojson;
2+
3+
import com.mapbox.core.TestUtils;
4+
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class GeometryTest extends TestUtils {
12+
13+
private static final String SAMPLE_GEOMETRY_COLLECTION = "sample-geometrycollection.json";
14+
15+
@Test
16+
public void fromJson() throws IOException {
17+
final String json = loadJsonFixture(SAMPLE_GEOMETRY_COLLECTION);
18+
Geometry geo = Geometry.fromJson(json);
19+
assertEquals(geo.type(), "GeometryCollection");
20+
}
21+
}

0 commit comments

Comments
 (0)