Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geometry from json #770

Merged
merged 3 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ subprojects {
}
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

dependencies {

Expand Down
23 changes: 23 additions & 0 deletions services-geojson/src/main/java/com/mapbox/geojson/Geometry.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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");
}
}