-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Description
Problem solved by the feature
GsonBuilder does not support overwriting the built-in adapters for Object and for JsonElement (and subclasses). This might not be obvious to users (see #1177).
gson/gson/src/main/java/com/google/gson/Gson.java
Lines 283 to 285 in 6d9c356
| // built-in type adapters that cannot be overridden | |
| factories.add(TypeAdapters.JSON_ELEMENT_FACTORY); | |
| factories.add(ObjectTypeAdapter.getFactory(objectToNumberStrategy)); |
(Side note: This only affects GsonBuilder; it is possible to overwrite these adapters when using @JsonAdapter on a field.)
Feature description
GsonBuilder.registerTypeAdaptershould throw an exception when the type isObjectorJsonElementor a subclass of itGsonBuilder.registerTypeHierarchyAdaptershould throw an exception when the type isJsonElementor a subclass of it
Additionally for both methods the documentation should be extended to specify that overwriting these adapters is not possible (but maybe mention that @JsonAdapter can be used instead).
Alternatives / workarounds
Only adjust the documentation (and Troubleshooting Guide) to mention that these adapters cannot be overwritten.
Or as proposed by #1177 let users overwrite the built-in adapters for these types.