28
28
import java .lang .reflect .Type ;
29
29
import java .text .DateFormat ;
30
30
import java .util .Date ;
31
+ import org .junit .Ignore ;
31
32
import org .junit .Test ;
32
33
33
34
/**
@@ -251,7 +252,10 @@ public void testSetStrictness() throws IOException {
251
252
public void testRegisterTypeAdapterForObjectAndJsonElements () {
252
253
String errorMessage = "Cannot override built-in adapter for " ;
253
254
Type [] types = {
254
- Object .class , JsonElement .class , JsonArray .class ,
255
+ Object .class ,
256
+ // TODO: Registering adapter for JsonElement is allowed (for now) for backward compatibility,
257
+ // see https://github.com/google/gson/issues/2787
258
+ // JsonElement.class, JsonArray.class,
255
259
};
256
260
GsonBuilder gsonBuilder = new GsonBuilder ();
257
261
for (Type type : types ) {
@@ -263,6 +267,22 @@ public void testRegisterTypeAdapterForObjectAndJsonElements() {
263
267
}
264
268
}
265
269
270
+ /**
271
+ * Verifies that (for now) registering adapter for {@link JsonElement} and subclasses is possible,
272
+ * but has no effect. See {@link #testRegisterTypeAdapterForObjectAndJsonElements()}.
273
+ */
274
+ @ Test
275
+ public void testRegisterTypeAdapterForJsonElements () {
276
+ Gson gson = new GsonBuilder ().registerTypeAdapter (JsonArray .class , NULL_TYPE_ADAPTER ).create ();
277
+ TypeAdapter <JsonArray > adapter = gson .getAdapter (JsonArray .class );
278
+ // Does not use registered adapter
279
+ assertThat (adapter ).isNotSameInstanceAs (NULL_TYPE_ADAPTER );
280
+ assertThat (adapter .toJson (new JsonArray ())).isEqualTo ("[]" );
281
+ }
282
+
283
+ @ Ignore (
284
+ "Registering adapter for JsonElement is allowed (for now) for backward compatibility, see"
285
+ + " https://github.com/google/gson/issues/2787" )
266
286
@ Test
267
287
public void testRegisterTypeHierarchyAdapterJsonElements () {
268
288
String errorMessage = "Cannot override built-in adapter for " ;
@@ -282,6 +302,20 @@ public void testRegisterTypeHierarchyAdapterJsonElements() {
282
302
gsonBuilder .registerTypeHierarchyAdapter (Object .class , NULL_TYPE_ADAPTER );
283
303
}
284
304
305
+ /**
306
+ * Verifies that (for now) registering hierarchy adapter for {@link JsonElement} and subclasses is
307
+ * possible, but has no effect. See {@link #testRegisterTypeHierarchyAdapterJsonElements()}.
308
+ */
309
+ @ Test
310
+ public void testRegisterTypeHierarchyAdapterJsonElements_Allowed () {
311
+ Gson gson =
312
+ new GsonBuilder ().registerTypeHierarchyAdapter (JsonArray .class , NULL_TYPE_ADAPTER ).create ();
313
+ TypeAdapter <JsonArray > adapter = gson .getAdapter (JsonArray .class );
314
+ // Does not use registered adapter
315
+ assertThat (adapter ).isNotSameInstanceAs (NULL_TYPE_ADAPTER );
316
+ assertThat (adapter .toJson (new JsonArray ())).isEqualTo ("[]" );
317
+ }
318
+
285
319
@ Test
286
320
public void testSetDateFormatWithInvalidPattern () {
287
321
GsonBuilder builder = new GsonBuilder ();
0 commit comments