Skip to content

GsonBuilder should throw exception when trying to register adapter for Object or JsonElement #2436

@Marcono1234

Description

@Marcono1234

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).

// 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.registerTypeAdapter should throw an exception when the type is Object or JsonElement or a subclass of it
  • GsonBuilder.registerTypeHierarchyAdapter should throw an exception when the type is JsonElement or 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions