Skip to content

Commit cf5dda8

Browse files
authored
API to access unrecognized fields (#1416)
1 parent 977adad commit cf5dda8

3 files changed

Lines changed: 91 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
Mapbox welcomes participation and contributions from everyone.
44

55
### main
6-
- Added roundtrip of unrecognized JSON properties [#1394](https://github.com/mapbox/mapbox-java/pull/1394)
6+
- Added roundtrip of unrecognized JSON properties. [#1394](https://github.com/mapbox/mapbox-java/pull/1394)
7+
- Added API to access unrecognized JSON properties. See `#getUnrecognizedProperty` and `#getUnrecognizedPropertiesNames` in each Directions API model object. [#1416](https://github.com/mapbox/mapbox-java/pull/1416)
78

89
### v6.4.1 - April 22, 2022
910
- Added correct escaping for `<white space>&<white space>` pattern in query parameters for `RouteOptions#toUrl`. [#1410](https://github.com/mapbox/mapbox-java/pull/1410).

services-directions-models/src/main/java/com/mapbox/api/directions/v5/models/DirectionsJsonObject.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import androidx.annotation.NonNull;
44
import androidx.annotation.Nullable;
55
import com.google.gson.GsonBuilder;
6+
import com.google.gson.JsonElement;
67
import com.mapbox.api.directions.v5.DirectionsAdapterFactory;
78
import com.mapbox.geojson.Point;
89
import com.mapbox.geojson.PointAsCoordinatesTypeAdapter;
910
import com.mapbox.auto.value.gson.SerializableJsonElement;
1011
import com.mapbox.auto.value.gson.UnrecognizedJsonProperties;
1112

1213
import java.io.Serializable;
14+
import java.util.Collections;
1315
import java.util.Map;
16+
import java.util.Set;
1417

1518
/**
1619
* Provides a base class for Directions model classes.
@@ -33,6 +36,41 @@ public String toJson() {
3336
return gson.create().toJson(this);
3437
}
3538

39+
/**
40+
* Use this method to access a JSON property that wasn't recognized during JSON serialization.
41+
* This may be useful to access experimental properties.
42+
* @param propertyName name of a json property
43+
* @return value of a requested property or null if the requested property doesn't exist.
44+
*/
45+
@Nullable
46+
public final JsonElement getUnrecognizedProperty(String propertyName) {
47+
JsonElement result = null;
48+
Map<String, SerializableJsonElement> unrecognizedProperties = unrecognized();
49+
if (unrecognizedProperties != null) {
50+
SerializableJsonElement property = unrecognizedProperties.get(propertyName);
51+
if (property != null) {
52+
result = property.getElement();
53+
}
54+
}
55+
return result;
56+
}
57+
58+
/**
59+
* Use this method to get names of unrecognized JSON properties.
60+
* @return names of unrecognized JSON properties or an empty set
61+
*/
62+
@NonNull
63+
public final Set<String> getUnrecognizedPropertiesNames() {
64+
Set<String> result;
65+
Map<String, SerializableJsonElement> unrecognizedProperties = unrecognized();
66+
if (unrecognizedProperties != null) {
67+
result = unrecognizedProperties.keySet();
68+
} else {
69+
result = Collections.emptySet();
70+
}
71+
return result;
72+
}
73+
3674
@Nullable
3775
@UnrecognizedJsonProperties
3876
abstract Map<String, SerializableJsonElement> unrecognized();

services-directions-models/src/test/java/com/mapbox/api/directions/v5/models/DirectionsResponseTest.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.GsonBuilder;
5-
import com.google.gson.JsonArray;
65
import com.google.gson.JsonElement;
76
import com.google.gson.JsonObject;
87
import com.google.gson.JsonPrimitive;
@@ -16,11 +15,13 @@
1615
import org.junit.Test;
1716

1817
import java.util.ArrayList;
19-
import java.util.Map;
18+
import java.util.Set;
2019

2120
import static com.mapbox.api.directions.v5.utils.MutateJsonUtil.mutateJson;
2221
import static org.junit.Assert.assertEquals;
2322
import static org.junit.Assert.assertNotNull;
23+
import static org.junit.Assert.assertNull;
24+
import static org.junit.Assert.assertTrue;
2425

2526
public class DirectionsResponseTest extends TestUtils {
2627

@@ -72,6 +73,49 @@ public void testToFromJsonWithMutatedResponse() throws Exception {
7273
assertEquals(mutatedJson, jsonFromObject);
7374
}
7475

76+
@Test
77+
public void accessUnrecognizedProperties() throws Exception {
78+
JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS);
79+
String unrecognizedPropertyName = "testUnrecognizedProperty";
80+
String unrecognizedPropertyValue = "test";
81+
directionsResponseJson.add(unrecognizedPropertyName, new JsonPrimitive(unrecognizedPropertyValue));
82+
DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString());
83+
84+
String value = response.getUnrecognizedProperty(unrecognizedPropertyName).getAsString();
85+
JsonElement notExistingProperty = response.getUnrecognizedProperty("notExisting");
86+
87+
assertEquals(unrecognizedPropertyValue, value);
88+
assertNull(notExistingProperty);
89+
}
90+
91+
@Test
92+
public void noUnrecognizedProperties() throws Exception {
93+
JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS);
94+
DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString());
95+
96+
JsonElement value = response.getUnrecognizedProperty("");
97+
Set<String> propertiesNames = response.getUnrecognizedPropertiesNames();
98+
99+
assertNull(value);
100+
assertEquals(0, propertiesNames.size());
101+
}
102+
103+
@Test
104+
public void getUnrecognizedPropertiesNames() throws Exception {
105+
JsonObject directionsResponseJson = readJsonObject(DIRECTIONS_V5_PRECISION6_FIXTURE_ARTIFICIAL_FIELDS);
106+
directionsResponseJson.add("1", new JsonPrimitive(1));
107+
directionsResponseJson.add("2", new JsonPrimitive(2));
108+
directionsResponseJson.add("3", new JsonPrimitive(3));
109+
DirectionsResponse response = DirectionsResponse.fromJson(directionsResponseJson.toString());
110+
111+
Set<String> properties = response.getUnrecognizedPropertiesNames();
112+
113+
assertEquals(3, properties.size());
114+
assertTrue(properties.contains("1"));
115+
assertTrue(properties.contains("2"));
116+
assertTrue(properties.contains("3"));
117+
}
118+
75119
@Test
76120
public void fromJson_correctlyBuildsFromJsonWithOptionsAndUuid() throws Exception {
77121
String json = loadJsonFixture(DIRECTIONS_V5_PRECISION6_FIXTURE);
@@ -158,4 +202,9 @@ public void fromToJsonForRouteWithSilentWaypoints() throws IOException {
158202

159203
assertEquals(initial, deserialized);
160204
}
205+
206+
private JsonObject readJsonObject(String file) throws IOException {
207+
Gson gson = new GsonBuilder().create();
208+
return gson.fromJson(loadJsonFixture(file), JsonObject.class);
209+
}
161210
}

0 commit comments

Comments
 (0)