Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does registerTypeAdapter for Object not work? #1187

Closed
onriv opened this issue Nov 4, 2017 · 2 comments
Closed

Why does registerTypeAdapter for Object not work? #1187

onriv opened this issue Nov 4, 2017 · 2 comments

Comments

@onriv
Copy link

onriv commented Nov 4, 2017

just tried to custom deserializer for object, and wrote the following code but it dosn't work:

JsonDeserializer<Object> objectJsonDeserializer = new JsonDeserializer<Object>() {

    @Override
    public Object deserialize(JsonElement json,
                              Type typeOfT,
                              JsonDeserializationContext context) throws JsonParseException {
        // can't reach this statement
        return null;
    }
};
gsonBuilder.registerTypeAdapter(Object.class, objectJsonDeserializer);

and all other deserializers for other types like map, list, and custom class work fine.

@onriv onriv changed the title Why registerTypeAdapter for Object does what? Why registerTypeAdapter for Object does not work? Nov 4, 2017
@onriv onriv changed the title Why registerTypeAdapter for Object does not work? Why does registerTypeAdapter for Object not work? Nov 4, 2017
@onriv
Copy link
Author

onriv commented Nov 4, 2017

found the reason at

factories.add(ObjectTypeAdapter.FACTORY);

so it seems just impossible to override the builtin deserializer for object.

But I want to deserialize decimal as BigDecimal, interger as Long, array as ImmutableList, object as ImmutableMap when wirting something like

gsonBuilder.create().fromJson("string", Object.class);

@NightlyNexus
Copy link
Contributor

duplicate of #1177.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants