File tree Expand file tree Collapse file tree
main/java/com/mapbox/geojson
test/java/com/mapbox/geojson Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 11package 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.
815 */
916public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments