Skip to content

Commit

Permalink
Initial fix for geocoder toJson issue (#698)
Browse files Browse the repository at this point in the history
* Initial fix for geocoder toJson issue

* Cleaned up context test class

* Fixed up GeoJson types

* Minfy fixtures

* fix merge issues
  • Loading branch information
Cameron Mace authored Jan 19, 2018
1 parent afe9e88 commit 8a5adf9
Show file tree
Hide file tree
Showing 33 changed files with 509 additions and 731 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ buildscript {

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
options.compilerArgs << '-Xlint:all' << '-Xlint:unchecked'
options.compilerArgs += ['-Xlint:all', '-Xlint:unchecked', 'autovaluegson.defaultCollectionsToEmpty:true']
}

allprojects {
Expand Down
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ext {

version = [
autoValue : '1.5',
autoValueGson : '0.6.0',
autoValueGson : '0.7.0',
junit : '4.12',
supportLibVersion: '26.1.0',
gson : '2.8.2',
Expand Down
13 changes: 7 additions & 6 deletions services-core/src/test/java/com/mapbox/core/TestUtils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.mapbox.core;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import com.google.gson.JsonParser;
import org.hamcrest.Matchers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand All @@ -11,18 +16,14 @@
import java.io.Serializable;
import java.util.Scanner;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class TestUtils {

public static final double DELTA = 1E-10;
public static final String ACCESS_TOKEN = "pk.XXX";

public void compareJson(String json1, String json2) {
public void compareJson(String expectedJson, String actualJson) {
JsonParser parser = new JsonParser();
assertEquals(parser.parse(json1), parser.parse(json2));
assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson)));
}

protected String loadJsonFixture(String filename) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.mapbox.api.geocoding.v5.models;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.SerializedName;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.gson.GeometryDeserializer;
import com.mapbox.geojson.gson.PointDeserializer;

import java.io.Serializable;

Expand All @@ -29,6 +35,14 @@ public static Builder builder() {
return new AutoValue_CarmenContext.Builder();
}

@SuppressWarnings("unused")
public static CarmenContext fromJson(@NonNull String json) {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
.create();
return gson.fromJson(json, CarmenContext.class);
}

/**
* ID of the feature of the form {index}.{id} where index is the id/handle of the data-source
* that contributed the result.
Expand Down Expand Up @@ -99,12 +113,30 @@ public static TypeAdapter<CarmenContext> typeAdapter(Gson gson) {
return new AutoValue_CarmenContext.GsonTypeAdapter(gson);
}

@SuppressWarnings("unused")
public String toJson() {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
.create();
return gson.toJson(this);
}

/**
* Convert current instance values into another Builder to quickly change one or more values.
*
* @return a new instance of {@link CarmenContext} using the newly defined values
* @since 3.0.0
*/
@SuppressWarnings("unused")
public abstract Builder toBuilder();

/**
* This builder can be used to set the values describing the {@link CarmenFeature}.
*
* @since 3.0.0
*/
@AutoValue.Builder
@SuppressWarnings("unused")
public abstract static class Builder {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.google.auto.value.AutoValue;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.mapbox.api.geocoding.v5.GeocodingCriteria.GeocodingTypeCriteria;
import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.gson.BoundingBoxDeserializer;
import com.mapbox.geojson.gson.BoundingBoxSerializer;
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
import com.mapbox.geojson.gson.GeometryDeserializer;
import com.mapbox.geojson.gson.GeometryTypeAdapter;
import com.mapbox.geojson.gson.PointDeserializer;
import com.mapbox.geojson.gson.PointSerializer;

import java.io.Serializable;
import java.util.List;
Expand All @@ -46,38 +42,40 @@
@AutoValue
public abstract class CarmenFeature implements Serializable {

@Expose
@SerializedName("type")
private static final String TYPE = "Feature";

/**
* Create a new instance of this class by using the {@link Builder} class.
*
* @return this classes {@link Builder} for creating a new instance
* @since 3.0.0
*/
public static Builder builder() {
return new AutoValue_CarmenFeature.Builder();
}

/**
* Create a CarmenFeature object from JSON.
*
* @param json string of JSON making up a carmen feature
* @return this class using the defined information in the provided JSON string
* @since 2.0.0
*/
@NonNull
public static CarmenFeature fromJson(@NonNull String json) {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapterFactory(GeocodingAdapterFactory.create());
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
gson.registerTypeAdapter(Point.class, new PointDeserializer());
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
return gson.create().fromJson(json, CarmenFeature.class);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Point.class, new PointDeserializer())
.registerTypeAdapter(Geometry.class, new GeometryDeserializer())
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
.create();
return gson.fromJson(json, CarmenFeature.class);
}

/**
* Create a new instance of this class by using the {@link Builder} class.
*
* @return this classes {@link Builder} for creating a new instance
* @since 3.0.0
*/
@NonNull
public static Builder builder() {
return new AutoValue_CarmenFeature.Builder()
.type(TYPE);
}

//
// Feature specific attributes
//

/**
* This describes the TYPE of GeoJson geometry this object is, thus this will always return
Expand All @@ -88,9 +86,8 @@ public static CarmenFeature fromJson(@NonNull String json) {
* @since 1.0.0
*/
@NonNull
public String type() {
return TYPE;
}
@SerializedName("type")
public abstract String type();

/**
* A {@link CarmenFeature} might have a member named {@code bbox} to include information on the
Expand Down Expand Up @@ -136,7 +133,9 @@ public String type() {
@NonNull
public abstract JsonObject properties();

//
// CarmenFeature specific attributes
//

/**
* A string representing the feature in the requested language, if specified.
Expand Down Expand Up @@ -281,13 +280,14 @@ public static TypeAdapter<CarmenFeature> typeAdapter(Gson gson) {
* @return a JSON string which represents this CarmenFeature
* @since 3.0.0
*/
@SuppressWarnings("unused")
public String toJson() {
GsonBuilder gson = new GsonBuilder();
gson.registerTypeAdapter(Point.class, new PointSerializer());
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer());
gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gson.create().toJson(this);
Gson gson = new GsonBuilder()
.registerTypeAdapter(Geometry.class, new GeometryTypeAdapter())
.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer())
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
.create();
return gson.toJson(this, CarmenFeature.class);
}

/**
Expand All @@ -296,6 +296,7 @@ public String toJson() {
* @return a new instance of {@link CarmenFeature} using the newly defined values
* @since 3.0.0
*/
@SuppressWarnings("unused")
public abstract Builder toBuilder();

/**
Expand All @@ -304,8 +305,12 @@ public String toJson() {
* @since 3.0.0
*/
@AutoValue.Builder
@SuppressWarnings("unused")
public abstract static class Builder {

// Type will always be set to "Feature"
abstract Builder type(@NonNull String type);

/**
* A Feature might have a member named {@code bbox} to include information on the coordinate
* range for it's {@link Feature}s. The value of the bbox member MUST be a list of size 2*n
Expand Down
Loading

0 comments on commit 8a5adf9

Please sign in to comment.