Skip to content

Commit 8a5adf9

Browse files
author
Cameron Mace
authored
Initial fix for geocoder toJson issue (#698)
* Initial fix for geocoder toJson issue * Cleaned up context test class * Fixed up GeoJson types * Minfy fixtures * fix merge issues
1 parent afe9e88 commit 8a5adf9

33 files changed

Lines changed: 509 additions & 731 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ buildscript {
2222

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

2828
allprojects {

gradle/dependencies.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ ext {
22

33
version = [
44
autoValue : '1.5',
5-
autoValueGson : '0.6.0',
5+
autoValueGson : '0.7.0',
66
junit : '4.12',
77
supportLibVersion: '26.1.0',
88
gson : '2.8.2',

services-core/src/test/java/com/mapbox/core/TestUtils.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package com.mapbox.core;
22

3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
import static org.junit.Assert.assertThat;
5+
import static org.junit.Assert.assertTrue;
6+
37
import com.google.gson.JsonParser;
8+
import org.hamcrest.Matchers;
49

510
import java.io.ByteArrayInputStream;
611
import java.io.ByteArrayOutputStream;
@@ -11,18 +16,14 @@
1116
import java.io.Serializable;
1217
import java.util.Scanner;
1318

14-
import static java.nio.charset.StandardCharsets.UTF_8;
15-
import static org.junit.Assert.assertEquals;
16-
import static org.junit.Assert.assertTrue;
17-
1819
public class TestUtils {
1920

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

23-
public void compareJson(String json1, String json2) {
24+
public void compareJson(String expectedJson, String actualJson) {
2425
JsonParser parser = new JsonParser();
25-
assertEquals(parser.parse(json1), parser.parse(json2));
26+
assertThat(parser.parse(actualJson), Matchers.equalTo(parser.parse(expectedJson)));
2627
}
2728

2829
protected String loadJsonFixture(String filename) throws IOException {

services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenContext.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package com.mapbox.api.geocoding.v5.models;
22

3+
import android.support.annotation.NonNull;
34
import android.support.annotation.Nullable;
45
import com.google.auto.value.AutoValue;
56
import com.google.gson.Gson;
7+
import com.google.gson.GsonBuilder;
68
import com.google.gson.TypeAdapter;
79
import com.google.gson.annotations.SerializedName;
10+
import com.mapbox.geojson.Geometry;
11+
import com.mapbox.geojson.Point;
12+
import com.mapbox.geojson.gson.GeometryDeserializer;
13+
import com.mapbox.geojson.gson.PointDeserializer;
814

915
import java.io.Serializable;
1016

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

38+
@SuppressWarnings("unused")
39+
public static CarmenContext fromJson(@NonNull String json) {
40+
Gson gson = new GsonBuilder()
41+
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
42+
.create();
43+
return gson.fromJson(json, CarmenContext.class);
44+
}
45+
3246
/**
3347
* ID of the feature of the form {index}.{id} where index is the id/handle of the data-source
3448
* that contributed the result.
@@ -99,12 +113,30 @@ public static TypeAdapter<CarmenContext> typeAdapter(Gson gson) {
99113
return new AutoValue_CarmenContext.GsonTypeAdapter(gson);
100114
}
101115

116+
@SuppressWarnings("unused")
117+
public String toJson() {
118+
Gson gson = new GsonBuilder()
119+
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
120+
.create();
121+
return gson.toJson(this);
122+
}
123+
124+
/**
125+
* Convert current instance values into another Builder to quickly change one or more values.
126+
*
127+
* @return a new instance of {@link CarmenContext} using the newly defined values
128+
* @since 3.0.0
129+
*/
130+
@SuppressWarnings("unused")
131+
public abstract Builder toBuilder();
132+
102133
/**
103134
* This builder can be used to set the values describing the {@link CarmenFeature}.
104135
*
105136
* @since 3.0.0
106137
*/
107138
@AutoValue.Builder
139+
@SuppressWarnings("unused")
108140
public abstract static class Builder {
109141

110142
/**

services-geocoding/src/main/java/com/mapbox/api/geocoding/v5/models/CarmenFeature.java

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@
33
import android.support.annotation.NonNull;
44
import android.support.annotation.Nullable;
55
import com.google.auto.value.AutoValue;
6-
import com.google.gson.FieldNamingPolicy;
76
import com.google.gson.Gson;
87
import com.google.gson.GsonBuilder;
98
import com.google.gson.JsonObject;
109
import com.google.gson.TypeAdapter;
11-
import com.google.gson.annotations.Expose;
1210
import com.google.gson.annotations.SerializedName;
1311
import com.mapbox.api.geocoding.v5.GeocodingCriteria.GeocodingTypeCriteria;
1412
import com.mapbox.geojson.BoundingBox;
1513
import com.mapbox.geojson.Feature;
1614
import com.mapbox.geojson.Geometry;
1715
import com.mapbox.geojson.Point;
18-
import com.mapbox.geojson.gson.BoundingBoxDeserializer;
1916
import com.mapbox.geojson.gson.BoundingBoxSerializer;
20-
import com.mapbox.geojson.gson.GeoJsonAdapterFactory;
2117
import com.mapbox.geojson.gson.GeometryDeserializer;
18+
import com.mapbox.geojson.gson.GeometryTypeAdapter;
2219
import com.mapbox.geojson.gson.PointDeserializer;
23-
import com.mapbox.geojson.gson.PointSerializer;
2420

2521
import java.io.Serializable;
2622
import java.util.List;
@@ -46,38 +42,40 @@
4642
@AutoValue
4743
public abstract class CarmenFeature implements Serializable {
4844

49-
@Expose
50-
@SerializedName("type")
5145
private static final String TYPE = "Feature";
5246

53-
/**
54-
* Create a new instance of this class by using the {@link Builder} class.
55-
*
56-
* @return this classes {@link Builder} for creating a new instance
57-
* @since 3.0.0
58-
*/
59-
public static Builder builder() {
60-
return new AutoValue_CarmenFeature.Builder();
61-
}
62-
6347
/**
6448
* Create a CarmenFeature object from JSON.
6549
*
6650
* @param json string of JSON making up a carmen feature
6751
* @return this class using the defined information in the provided JSON string
6852
* @since 2.0.0
6953
*/
54+
@NonNull
7055
public static CarmenFeature fromJson(@NonNull String json) {
71-
GsonBuilder gson = new GsonBuilder();
72-
gson.registerTypeAdapterFactory(GeocodingAdapterFactory.create());
73-
gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
74-
gson.registerTypeAdapter(Point.class, new PointDeserializer());
75-
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer());
76-
gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer());
77-
return gson.create().fromJson(json, CarmenFeature.class);
56+
Gson gson = new GsonBuilder()
57+
.registerTypeAdapter(Point.class, new PointDeserializer())
58+
.registerTypeAdapter(Geometry.class, new GeometryDeserializer())
59+
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
60+
.create();
61+
return gson.fromJson(json, CarmenFeature.class);
62+
}
63+
64+
/**
65+
* Create a new instance of this class by using the {@link Builder} class.
66+
*
67+
* @return this classes {@link Builder} for creating a new instance
68+
* @since 3.0.0
69+
*/
70+
@NonNull
71+
public static Builder builder() {
72+
return new AutoValue_CarmenFeature.Builder()
73+
.type(TYPE);
7874
}
7975

76+
//
8077
// Feature specific attributes
78+
//
8179

8280
/**
8381
* This describes the TYPE of GeoJson geometry this object is, thus this will always return
@@ -88,9 +86,8 @@ public static CarmenFeature fromJson(@NonNull String json) {
8886
* @since 1.0.0
8987
*/
9088
@NonNull
91-
public String type() {
92-
return TYPE;
93-
}
89+
@SerializedName("type")
90+
public abstract String type();
9491

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

136+
//
139137
// CarmenFeature specific attributes
138+
//
140139

141140
/**
142141
* A string representing the feature in the requested language, if specified.
@@ -281,13 +280,14 @@ public static TypeAdapter<CarmenFeature> typeAdapter(Gson gson) {
281280
* @return a JSON string which represents this CarmenFeature
282281
* @since 3.0.0
283282
*/
283+
@SuppressWarnings("unused")
284284
public String toJson() {
285-
GsonBuilder gson = new GsonBuilder();
286-
gson.registerTypeAdapter(Point.class, new PointSerializer());
287-
gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer());
288-
gson.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT);
289-
gson.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
290-
return gson.create().toJson(this);
285+
Gson gson = new GsonBuilder()
286+
.registerTypeAdapter(Geometry.class, new GeometryTypeAdapter())
287+
.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer())
288+
.registerTypeAdapterFactory(GeocodingAdapterFactory.create())
289+
.create();
290+
return gson.toJson(this, CarmenFeature.class);
291291
}
292292

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

301302
/**
@@ -304,8 +305,12 @@ public String toJson() {
304305
* @since 3.0.0
305306
*/
306307
@AutoValue.Builder
308+
@SuppressWarnings("unused")
307309
public abstract static class Builder {
308310

311+
// Type will always be set to "Feature"
312+
abstract Builder type(@NonNull String type);
313+
309314
/**
310315
* A Feature might have a member named {@code bbox} to include information on the coordinate
311316
* range for it's {@link Feature}s. The value of the bbox member MUST be a list of size 2*n

0 commit comments

Comments
 (0)